I am a C++ programmer and I rarely have to deal with GCC “C”. I am currently converting some code from C++ to C for use with the GCC compiler.
In C++ I would use the following source code. Note that I would use a class for scope.
C++ Source
class Card {
public:
enum Suit {
Diamonds, Hearts, Clubs, Spades
};
};
class Weapon {
public:
enum WeaponType {
Rocks, Clubs, Guns
};
};
int main () {
Suit a = Card::Clubs;
WeaponType b = Weapon::Clubs
}
In “C” with no classes and no way of differentiation between the two different “clubs” keywords. I get the following error message when trying to compile this
error C2365: ‘Clubs’ : redefinition; previous definition was
‘enumerator’
C Source
enum Suit {
Diamonds, Hearts, Clubs, Spades
};
enum WeaponType {
Rocks, Clubs, Guns
};
int main () {
Suit a = Clubs;
WeaponType b = Clubs
}
So my questions are
- How do I declare two different enum in the same file with the same keyword?
- How do I add scope to the enum so I can assign different variables different enum with the same enum name?
Do it just like the old days: