0

Whenever I try to create a string in my header file I get the error: 'string' is not a member of 'std'. I am using the Microsoft Visual c++ express 2010 compiler. Here is my header file:

using namespace std;

class Person
{
private:
 string name;
 string p_number;

public:
 Person();
 Person(string, string);
 string get_number();
 string get_name();
};

I am a decent java programmer who just started learning c++

1
  • @Tom: you should have added that as an answer Commented Nov 30, 2010 at 12:34

5 Answers 5

4

Do you also have #include <string> in your header? You need it for the declarations of the string classes.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this solved my problem. Why don't I need #include <string> in my main file? The only thing my main file includes in <iostream>
@Daniel: If your main file is including your header file, you are effectively including <string> in the main file too.
@Daniel: In C++, standard headers are allowed (but not required) to include other standard headers. So if your main file includes <iostream>, it's presumably getting <string> too because your implementation happens to have a dependency there.
@Daniel: Based off of what Steve and Niall said, you must remember your program's structure and hierarchy. E.g. <iostream> is included in something.h and something.h is included in main.cpp but not something_else.cpp. Then iostream is effectively included in any file where something.h is included (in this case main.cpp) and not those files which do not (in this case something_else.cpp). So if you had no reason to include something.h into something_else.cpp, you would simply re-include <iostream> if you needed the standard functionality in there as well. Good luck!
2

You must include the string file like this:

#include <string>

to use std::string .

It's something like import in Java, except that Java imports classes / namespaces, C++ imports libraries or header files.

Comments

1

You need to #include <string>.

Comments

1

Put

#include <string>

at the top of the file.

Comments

0
using std::string;

will not include unnecessary declarations and definitions present in string header and will be helpful only for compiler's lookup and would compile fine, I usually prefer following instead of including whole header files:

using std::cout;
using std::cin;
using std::endl;

*For example purpose only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.