472,358 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

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_string<char, std::char_traits<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::string const & p)
:std::string(p)
{}

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

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 2715
"Heiko Hund" <he***@ist.eigentlich.net.invalid> 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_string<char, std::char_traits<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::operator 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::operator 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::string const & p)
:std::string(p)
{}

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

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
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...
9
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...
22
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; ...
2
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...
1
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)...
5
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...
4
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...
10
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
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.