Question:
How do I do a forward declaration for the STL ‘string’ class? For example:
// Foo.hppclass string;class Foo{ string f1(); void f2(const string& str); ...}
On various compliers (MS, Sun, GNU) this produces error messages to the effect of “invalid class definition” or “conflicting class definition.”
Answer:
You can’t forward declare std::string because it’s not the real name of this class; it’s a typedef name that hides the cumbersome syntax of the following template instance:
typedef basic_string,
allocator> string;
Because the compiler must know the exact type of a parameter in order to get the functions named correctly managed, you can’t use a typedef name in a forward declaration, unless you provide the typedef declaration as well. In other words, it’s OK to use a typedef name in a forward declaration as long as the compiler can see the actual type behind the typedef name. The actual type of the typedef std::string appears in
Seemingly, you can place the typedef declaration given above instead of #including the