undeclared identifier

Can somebody please explain to me what I need to do to fix this problem?
1
2
3
4
5
Time::Time() //default constructor sets the hour and minute
{
	hour = 0;
	minute = 0;
}


If I just add int in front of it maybe?

Ok I figured this one out. I was right about adding int in front, I think, but what about this other one?
1
2
3
4
5
Time::Time (int hr, int min)
{
	hr = hour;
	min = minute;
}


My guess is probably place string in front?
Last edited on
Be more descriptive. Which is the undeclared indentifier? hour and minute? If that's the case, you haven't defined them in your class or failed to include the header file, possibly.
show the whole class

but usually constructors should be inside the class and must be public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Time
{
Time()
{
//hour & minutes must be declared data members 
hour = 0;
minute = 0;
}

//declared data members here
int hour;
int minute;

};

Header? What header file would I add in? Something like Time.h?
Here is the whole class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Time
{
public:
	Time(); //constructor
	Time(int, int);	//constructor with parameters
	void setHour(int);
	void setMinute(int);
	int getHour();
	int getMinute();
	void printTime();
private:
	int hr;
	int min;
};
There is the problem...in the class, you define two ints: hr and min, and yet in the constructor you use hour and minute.

EDIT: Oh wait, I see what you did, and it's wrong. Don't declare the constructor's parameters to have the same name as members of the class; that just "hides" them unless you use this-> to explicitly reference the member variables.
I'm sorry Zhuge, I don't think I understand what you mean. I know - damn noobies!
You have "hr" and "min" in your class.
You also have two parameters to your constructor called "hr" and "min".
In your constructor, you assign to both "hr" and "min".

Since you have pairs of variables, each named "hr" and "min", the compiler has to decide which one you are referring to. In this case, it assigns to the parameters, accomplishing nothing for you since the class's variables are left unchanged.
Topic archived. No new replies allowed.