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.
snazzy154Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Since

#include <stdio.h>

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


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");
}

compiles fine, I assume that you are not including teh header file that declares the enum. Actually

extern enum Suit;

has not the effect you desire, you need to

#include "the_header_that_has_suit.h"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snazzy154Author Commented:
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
novitiateCommented:
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_
jkrCommented:
This should not happen. Could you post the relevant code?
snazzy154Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.