I'm trying to declare a string variable as a private class variable. However, when I compile, I get " 'string' does not name a type in function int main() ".
Here is my code:
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <cstdlib>
#include <iostream>
#include <string>
#include "clock.h"
usingnamespace CH02_PROB18;
usingnamespace std;
int main()
{
//Clock my_clock();
cout << "Your clock has been set to ";
cout << my_clock.get_hour << ":";
cout << my_clock.get_minute << " ";
system("PAUSE");
return EXIT_SUCCESS;
}
Thanks. You're right. I didn't call the function correctly. I also didn't take out the commenting on the Clock my_clock() line. I changed both of those.
Unfortunately, I still get the exact same error. I even added std:: before use of the string ampm; declaration. It does not like my simple variable declaration "std::string ampm;" I still don't know why. What am I missing? I have the #include <string> in the main file, so I don't see why it doesn't let me declare this variable. This is getting frustrating.
Never mind. Using the scope resolution "std::string ampm" DID solve the problem. I had forgotten to uncomment my function declaration (std::string get_ampm() const;", which was why it still wouldn't compile.
For posterity, the reason that my error occurred (in addition to what eidge pointed out above) was that I needed to use a scope resolution operator before the type string since you can't use a using statement in a header file.
I have seen the question asked many times in other posts, but it's not always answered clearly for beginners. So, from one beginner to others, when you see this error:
'string' does not name a type in function int main()
try adding "std::" right before your string variable declaration, particularly in your header file.
It's not that it won't work. My textbook says to never put a using statement in a header file. I've also seen this stated on other posts. Apparently, it can cause conflicts. I'm not experienced enough to say why.