• Popular Now

• 9
• 9
• 11
• 13
• 9

This topic is 3479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey again. I'd like to know why my compiler fails to recognize the std namespace in the following two cases. Neither of the two examples will compile and both error out (see below). The code is in a header file that is #include-d inside another cpp file. Seems almost like as if the std namespace wasn't being defined in string.h, but I always thought string *was* a part of "std" namespace. I'm getting the same result in msvc++6 and msvc++9. Why is this so and more importantly what's the solution? I've figured that including some other things like <vector> will solve the problem, but that doesn't answer my question.
#ifndef SOME_HEADER
#include <string.h>
using namespace std;
string asd;
#endif


#ifndef SOME_HEADER
#include <string.h>
std::string asd;
#endif


Errors: d:\(path-to-file)\aaa.h(6) : error C2653: 'std' : is not a class or namespace name d:\(path-to-file)\aaa.h(6) : error C2146: syntax error : missing ';' before identifier 'asd' d:\(path-to-file)\aaa.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\(path-to-file)\aaa.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [Edited by - Blednik on October 10, 2008 2:00:04 PM]

Share on other sites
string.h is obsolete and doesn't make use of namespaces, just #include <string>;

EDIT: Oh, and 'using namespace' in a header file is probably bad practice.

Share on other sites
std::string is part of <string>, not <string.h>

<string.h> includes C String functionality, which you'll probably want to avoid.

Share on other sites
Ah, nice. I didn't know there was a <string> file too. ;) Solved.