473,903 Members | 4,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

private inheritance from std::string error

Hi,

I do not understand the deeper reason for the following compiler error

$ g++ test.cpp
test.cpp: In function `int main()':
test.cpp:41: error: `std::basic_str ing<char, std::char_trait s<char>,
std::allocator< char> >' is an inaccessible base of `Path'

I'm using Debians

$ g++ --version
g++ (GCC) 3.3.5 (Debian 1:3.3.5-8)
$ dpkg -l libstdc++5
ii libstdc++5 3.3.5-8 The GNU Standard C++ Library v3

Here the source code that produces the problem:

#include <string>

class Path : private std::string
{
public:
Path(std::strin g const & p)
:std::string(p)
{}

Path(Path const & rhs)
:std::string(rh s)
{}

operator std::string() const
{
return static_cast<std ::string>(*this );
}

operator std::string const() const
{
return static_cast<std ::string const>(*this);
}

operator std::string const &() const
{
return static_cast<std ::string const &>(*this);
}

operator char const *() const
{
return c_str();
}
};

int main()
{
Path p("foo");
Path p2(p);
char const * c(p);

std::string s(p); // <- error

return 0;
}

Any enlightenment is highly appreciated.

Thanks
Heiko
Jul 23 '05 #1
3 2863
"Heiko Hund" <he***@ist.eige ntlich.net.inva lid> wrote in message
news:cu******** **@schlund.de.. .
Hi,

I do not understand the deeper reason for the following compiler error

$ g++ test.cpp
test.cpp: In function `int main()':
test.cpp:41: error: `std::basic_str ing<char, std::char_trait s<char>,
std::allocator< char> >' is an inaccessible base of `Path'

I'm using Debians

Posting your code to the Comeau compiler,
http://www.comeaucomputing.com/pcgi-bin/compiler.cgi,
gives the following warnings:

"ComeauTest .c", line 14: warning: "Path::oper ator std::string() const" will
not be called for implicit or explicit conversions

I guess the base class conversion takes priority over
whater operator you declare.
How about using a private member instead?

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #2
Ivan Vecerina wrote:
Posting your code to the Comeau compiler,
http://www.comeaucomputing.com/pcgi-bin/compiler.cgi,
gives the following warnings:

"ComeauTest .c", line 14: warning: "Path::oper ator std::string() const"
will not be called for implicit or explicit conversions

I guess the base class conversion takes priority over
whater operator you declare.
Hmm, I wonder why. My conversion operator should be more specific than a
implicit cast to a base class. Especially since private inheritance
expresses a 'has a' relation, afaik.
How about using a private member instead?


Though about it, but wanted to avoid simple wrapper methods to access
std::string functionality. I think I'll have to go that way now. A pity!

Thanks again
Heiko
Jul 23 '05 #3

Heiko Hund wrote:
#include <string>

class Path : private std::string
{
public:
Path(std::strin g const & p)
:std::string(p)
{}

Path(Path const & rhs)
:std::string(rh s)
{}

operator std::string() const
{
return static_cast<std ::string>(*this );
}

operator std::string const() const
{
return static_cast<std ::string const>(*this);
}
};


Derived-to-base conversions are selected over user-defined
conversions, and access checking is performed after the
conversion sequence is selected. That's why your private base
class hides the user-defined conversion, even though it's
private.

HTH,
Michiel Salters

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1894
by: Dave | last post by:
Can anybody tell me why the code below should produce the error below? I'm going nuts here! Am I missing something incredibly obvious and simple? std::string::size_type idx; std::string one_line; .. .. .. idx = one_line.find('\t', 0);
9
10287
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not only seems to be a bad practice but also creates a bug on MacOSX panther (gcc 3.3).
22
13353
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
2
1811
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include "BaseHeader.h" /****************************************************/ /* Constructors/Destructor */
1
3772
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003) class library and type in the following code below: #include <map> #include<string>
5
5434
by: sean345 | last post by:
While compiling my program I get the following g++ error: error: no matching function for call to `ErrorLog::file_error(std::string&, std::string&, const char)' note: candidates are: void ErrorLog::file_error(const std::string&, int, const std::string&) The error line is: log.file_error(filename, line, "variable is not assigned a value");
4
11241
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and with GCC (Cygwin and Linux) and my problem is when i try do this int main(int argc, char **argv) { array<std::stringa(10); a = "Huhuhu"; <--- with gcc i got a crash !
10
7332
by: mr_sorcerer | last post by:
Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn't check null pointers?
29
23264
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to write to the file: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I don't know what I am doing wrong. I have posted my entire program
0
9999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11285
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10878
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10986
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.