Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax check 1

Status
Not open for further replies.

chipperMDW

Programmer
Mar 24, 2002
1,268
US
I can't tell why VC++ doesn't like this code:

[tt]#include <string>

class Foo : public std::string
{
public:
Foo()
: string()
{}
};
[/tt]

VC++ seems to think that string is not a base or member of Foo. Putting &quot;std::&quot; before &quot;string&quot; in the initializer list produces the same error. However, putting &quot;using std::string;&quot; before the class declaration causes it to work fine. The code also works fine with any arbitrary classes and namespaces (say blah::Bar in place of std::string).

Is this a VC++ quirk, or am I using incorrect syntax? In the case of the latter, what would the correct syntax be (without using &quot;using&quot;)?
 

VC++ seems to think that string is not a base or member of Foo. That's because it isn't. What you are doing will never work. &quot;string.h&quot; has declarations for string manipulation functions. It's not even a class. You're trying to derive Foo from it. Use CString if you need string functions in Foo.

Another tip, you have #include <string>, always put the '.h'. There are times when other include file will need the .h or you'll include the wrong file. You'll spend hours trying to figure out what's wrong.

Brother C
 
You're right that <string.h> doesn't have a string class. You're also right that leaving off the .h will get you a completely different file. That's why I used not <string.h>, but <string>, a completely different header, which does have a string type.

string is a base of Foo. What I'm doing will work and does work (which I mentioned in my post).

And yes, ok, technically, string itself is not a class; it's a typedef of the template class basic_string<char>. That's close enough.

My question still stands.
 
That's really a strange problem. I vote for a Bug in the compiler. Look at the following example, which compiles w/o problems:

namespace myspace
{
class Huhu
{
};

typedef Huhu Huhu2;
}


class Foo : public myspace::Huhu
{
public:
Foo()
: myspace::Huhu()
{}
};

However, when you try to use Huhu2 instead of Huhu as a base class, the same error as in your example appears. The similarity to your code is that 'string' is typedef'd from basic_string<...>.

Something with the typedef processing seems to be broken in the compiler. Other opinions?
 
ok... I just took a look into the documentation (yes, I do it sometimes!), and found the following statement:

&quot;Names declared using typedef occupy the same name space as other identifiers&quot;

So your code is correct and MSVC6 is wrong ;-) ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top