views:

366

answers:

2

In my header file I'm getting the

error: ‘string’ has not been declared

error but at the top of the file I have #include <string>, so how can I be getting this error?

+14  A: 

string resides in the std namespace, you have to use std::string or introduce it into the scope via using directives or using declarations.

Georg Fritzsche
A: 

Like Georg said, string resides in the std namespace and is referred to as std::string. If you don't want to go around and change all your code, you can:

#define string std::string

Or (and don't quote me on this) there should be a directive like namespace std to declare that it uses the std namespace by default.

amphetamachine
No, don't - please. This could break all kind of things.
Georg Fritzsche
If you don't want to change too much code rather use `using std;` instead. This is a valid feature of C++. Using the preprocessor to "fix" the problem *will* cause all kinds of problems. Please don't do this.
bluebrother
@bluebrother: You mean `using namespace std;` or `using std::string;`.
KennyTM
@KennyTm: I presume that is what @amphetamachine meant, but given @Phenom is working on a header file, polluting the global namespace with `using` statements should be discouraged.
Johnsyweb
Lol this is how "perl hackers" like @amphetamachine do it. String replacement! haha
Johannes Schaub - litb