Problem with string variable in a class

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"
using namespace CH02_PROB18;
using namespace 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;
}


clock.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CH02_PROB18_H
#define CH02_PROB18_H

namespace CH02_PROB18
{
    class Clock
    {
        public:
            //DEFAULT CONSTRUCTOR
            Clock(); //Initializes a new Clock to 00:00
            
            //CONSTANT MEMBER FUNCTIONS
            int get_hour() const;
            int get_minute() const;
            //string get_ampm() const;
        
        private:
            int hour, minute;
            string ampm;
    };
}
#endif 


I don't see why this is happening. Can anyone help? Thanks.
Last edited on
Your code is set like this:

my_clock.get_hour

you forgot the "()", it should be like this:

my_clock.get_hour()

That should be it. ;)
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.
Last edited on
You'll need to include <string> in every file that uses string declarations.

If that doesn't solve the problem, post the entire code and I'll try to see what's wrong.
You'll need to include <string> in every file that uses string declarations.


Better yet... since <string> is a dependency of clock.h, clock.h should be #including <string>.

See section 4 of the below article:

http://cplusplus.com/forum/articles/10627/#msg49679
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.
That's odd. I use "using namespace std" in my headers and they work fine.

using C::B with mingw compiler.

It might not be a good idea but it works, I haven't really read anything about it..
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.
Neither am I.

If you find anything on the reasons why, leave it here so that I can take a look at it, please.
Topic archived. No new replies allowed.