enum in class

Hi guys,

Looking for a little help with enum. Here's the code:

No matter where I put the decleratiron "compensation comp;", in the constructor for example, or the input function, it's not recognized across both input and the print function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Employee{

public:
	Employee(){}
	void input(){
		char choice;
		compensation comp;//issue here in decleration
		cout<<"Enter the name: ";
		cin>>name;
		cout<<"'d' for Daily\n'w' for Weekly\n'm' for Monthly\n'y' for yearly\n";
		cin>>choice;
		switch(choice){
		case 'd': comp=Daily;break;
		case 'w': comp=Weekly;break;
		case 'm': comp=Montly;break;
		case 'y': comp=Yearly;break;
		default: break;
		}
		
	}
	void print(){
		switch(comp){//get error here
		case Daily:cout<<"Daily\n";break;
		case Weekly:cout<<"Weekly\n";break;
		case Montly:cout<<"Montly\n";break;
		case Yearly:cout<<"Yearly\n";break;
		default: break;
		}
	}
private:
	char name[60];
	enum compensation{Daily, Weekly, Montly, Yearly};
};

int main()
{
	Employee e1;
	e1.input();
	e1.print();
	_getch();	
    return 0;
}


However this works! But it's all buried in one functions which is not what I want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Employee{

public:
	Employee(){}
	void input(){
		char choice;
		compensation comp;
		cout<<"Enter the name: ";
		cin>>name;
		cout<<"'d' for Daily\n'w' for Weekly\n'm' for Monthly\n'y' for yearly\n";
		cin>>choice;
		switch(choice){
		case 'd': comp=Daily;break;
		case 'w': comp=Weekly;break;
		case 'm': comp=Montly;break;
		case 'y': comp=Yearly;break;
		default: break;
		}
		switch(comp){
		case Daily:cout<<"Daily\n";break;
		case Weekly:cout<<"Weekly\n";break;
		case Montly:cout<<"Montly\n";break;
		case Yearly:cout<<"Yearly\n";break;
		default: break;
		}
	}
	void print(){
	
	}
private:
	char name[60];
	enum compensation{Daily, Weekly, Montly, Yearly};
};

int main()
{
	Employee e1;
	e1.input();
	e1.print();
	_getch();	
    return 0;
}


Can anyone tell me where to declare the compensation, so it's recognized in all functions?

THanks,

Mike
Sounds like you want it as a member of the class. So put compensation comp; between lines 31 and 32.
Define

enum compensation{Daily, Weekly, Montly, Yearly} comp;

and remove

compensation comp;//issue here in decleration

from the input.
Well that did the trick. But I did not think it was possible to initialize inside the private member area of the class. Is "compensation comp" not a sort of initialization?

closed account (zb0S216C)
It looks like the compiler cannot find the compensation enumeration you're trying to base comp on. For this to work, the enumeration, compenstion, must be declared before declaring comp. For instance:

1
2
3
4
5
6
7
struct Employee
{
    enum Compensation
    {
        // ...
    } comp_;
};

Wazzak
Well that did the trick. But I did not think it was possible to initialize inside the private member area of the class. Is "compensation comp" not a sort of initialization?


It's not any different than what you're doing on line 31.
Topic archived. No new replies allowed.