Question

HeroicZach's avatar

Why does C++ not allow me to use strings in a class header file?

Asked by HeroicZach (112points) | asked 2 months ago | 1 response | “Great Question” (0points) | Flag as…

I have code that looks like this:

public:
myClass(string inWord); // value constructor

Up above the class in the class header file, I have #include <string>, so C++ should know what I’m talking about. Unfortunately, I ALWAYS get this error message:

15 myClass.h expected `)’ before “inWord”

Further down in the class, I have:
bool myFunction(string guessWord);
but I get the following error message:
22 myClass.h expected `;’ before ’(’ token

Why is this happening? If I comment out all the references to string the program compiles, and even when I engage some back in, the program starts to compile – it’s only when I create the value constructor in the .cpp file that matches with this .h that the program explodes in errors. Even when I comment out the value constructor implementation in the .cpp file, clean, then recompile, it still explodes in errors until I remove all string references.

Your help is 110% greatly appreciated.

Observing members: 0 Composing members: 0

Answers

HeroicZach's avatar

OK, I think I’ve got this figured out!

I already declared “using namespace std;” in my main.cpp file, but in the .h, I did not, so technically I was supposed to be calling std::string. What a stupid mistake!

Answer this question

Login

or

Join

to answer.
Your answer will be saved while you login or join.

Have a question about something? Ask Fluther!