Link to home
Start Free TrialLog in
Avatar of snazzy154
snazzy154

asked on

Problem with enumeration scope and classes

In one file I have an enumeration defined, say

enum Suit { Diamonds, Hearts, Clubs, Spades };

in another file, I have something like:

extern enum Suit;
class foo {
   public:
   foo();
   Suit getSuit();
   void setSuit(Suit a);
   void bar();

   private:
   Suit _suit;
};

Suit foo::getSuit() { return _suit; }
void foo::setSuit(Suit a) { _suit = a; }

void foo::bar() {
  if( this->getSuit() == Diamonds )
      printf("Diamond found");
}


this results in compile errors of:
error C2065: 'Diamonds' : undeclared identifier
error C2371: 'Diamonds' : redefinition; different basic types


same thing happens if, instead of this->getSuit(), I use _suit or getSuit(); also, using (enum Suit)Diamonds instead of just Diamonds does not help either.

I tried putting my extern inside the class definition but that just caused different problems. The setSuit and getSuit functions do not give me any problems.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snazzy154
snazzy154

ASKER

That compiles, but it seems for me that the if statement never gets executed in my code, even if there is a Diamonds. It will if I replace it with the number 260. (This is just a shortened example, as my actual program contains a lot more code.)

In fact, in my actual code I cannot give any value to my enumeration.

It seems my enumerations start out at 260. Even if I have something like enum Suits { Diamonds = 60000, Hearts, Spades = 92, Clubs = 200 }; -- it seems that the values are still 260, 261, 262, and 263. I found this out by using printf("%d", var).

What could be messing with my enumeration values?

Thanks
just my two cents...

Remember enum is a declaration not a definition. extern is used in cases where definition is else where. So in your case, as Jkr has suggested, you need to put your enum declaration in a .h file and include it where ever you need

_novi_
This should not happen. Could you post the relevant code?
I found out why. I'm using Lex and Yacc and basically they defined the variables in my enumeration already. So essentially my enumeration consisted of { 260, 261, 262, 263 }.

Thanks for the help.