Defining a class method with string return type

I'm sure this must be a real noob question, but I've gone round and round in circles and can't see anything wrong here...

I'm writing a class; in the header file, I add a public method:
1
2
3
4
5
6
7
8
9
#include <string>

...

class MyClass {
    ...
    string toString();

}


In spite of the include statement, I get this compile error...

error: 'string' does not name a type


Huh???
Last edited on
Wow! Less than a minute after posting the question, I figure out the answer. I should've posted sooner.

What I was missing was:

using namespace std;

But I don't understand why that's needed for string to be recognized. Isn't string in its own namespace?
closed account (zb0S216C)
If I read the C++ Standard document right, the "string" is declared in the "std" namespace. I think the include directive: #include<string> only includes the string's prototypes.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf

Go to: section 21( String's Library ).
closed account (1yR4jE8b)
If you don't want to use the using namespace std; directive (this imports everything from the std namespace into the global namespace, typically don't want to pollute the global namespace that much)

Then just use either using std::string instead of the full namespace directive OR (this is the preferred way when writing classes in header files) Instead of string using std::string
Last edited on
Topic archived. No new replies allowed.