trying to learn enum

right now my main problem is that the program is not picking up on the fact that I initialized my enumerations. Now I barely understand anything about enum and why they convert perfectly good strings into integers so if anyone can either redirect me to some easy to understand information about them or can answer why this isn't considering the variables initialized I would appreciate it. Otherwise I can post the rest of my code if needed. My whole C++ class except for one guy is stumped by this stuff...


//I declared the enumerations here

class Flag
{

private:
enum eFlagColor1 { red = 1, blue };
enum eFlagColor2 { white = 1, gold };

public:
Flag();
Flag2( eFlagColor1, eFlagColor2 );
SetFlagColors1( eFlagColor1, eFlagColor2 );
SetFlagColors2( int, int );
GetFlagColors( int *, int * );

};


//and the enumerations were initialized here in the flag constructor

Flag::Flag()
{
eFlagColor1 eFlagColor1 = red;
eFlagColor2 eFlagColor2 = white;
}


//but for some reason below when I add the if statements to verify that the enum is set to red, it says I need to initialize eFlagColor1 and eFlagColor2

Flag::SetFlagColors1( eFlagColor1, eFlagColor2 )
{

eFlagColor1 eFlagColor1;
eFlagColor2 eFlagColor2;

if ( eFlagColor1 == red )
SetFlagColors2( eFlagColor1, blue );

if ( eFlagColor2 == white )
SetFlagColors2( eFlagColor2, gold);

}
An enumerated type allows you to specify text values to replace an integer. This is useful when you want to assign "codes" to return integers. E.g. you can create multiple enums that represent different error conditions then return the one that occurred in a function.

Your problem here is that you are thinking of an enum as an object, but it is not. An enum is simply another data type (like a string, int, double etc) that can have only limited different values (e.g bool can only be true/false). These values are the ones that you define.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum EFlagColor1 { RED, BLUE, WHITE, GOLD };

int main() {

 EFlagColor myFlag = RED;

 if (myFlag == BLUE)
  cout << "MyFlag is blue" << endl;
 else if (myFlag == RED) {
  cout << "MyFlag is Red" << endl;
 else
  cout << "MyFlag is another color" << endl;
 
 return 0;
}
Alright I guess that helps a bit, I'll try it that way.
Topic archived. No new replies allowed.