Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ how to have same enum members name in different enum names without getting err:redefinition; previous definition was 'enumerator'

Tags:

c++

enums

I have config file that I include in all my files there I have different enums but inside each enum there are same element names for example: config.h

enum GameObjectType {      NINJA_PLAYER  }; enum GameObjectTypeLocation {     NONE,     MASSAGE_ALL,  //this is for ComponentMadiator     NINJA_PLAYER   }; 

But when I try to compile the project with calling the enums with the proper enum name

m_pNinjaPlayer = (NinjaPlayer*)GameFactory::Instance().getGameObj(GameObjectType::NINJA_PLAYER);     ComponentMadiator::Instance().Register(GameObjectTypeLocation::NINJA_PLAYER,m_pNinjaPlayer); 

I am getting compilation error:

error C2365: 'NINJA_PLAYER' : redefinition; previous definition was 'enumerator' (..\Classes\GameFactory.cpp) 2>          d:\dev\cpp\2d\cocos2d-x-3.0\cocos2d-x-3.0\projects\lettersfun\classes\config.h(22) : see declaration of 'NINJA_PLAYER' 

How can I keep in the config.h several enums with different names BUT with same elements names ?

like image 413
user63898 Avatar asked Apr 25 '14 09:04

user63898


People also ask

Can two enums have the same name?

No two enum members can have the same name. Each enum member has an associated constant value.

Can different enumeration may have same name?

1. Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Can enum and class have same name?

The enum value BAD resides in the general identifier scope, while the class type BAR resides in the class identifier scope. That is the reason why you are allowed to have both an enum value and a class with the same name: both names do not collide.

Should enums be passed by const reference?

"Plain" enums and enum class objects both are of integral type, so the decision of passing by const reference or by value is just the same as if you did for other arguments of integral type.


2 Answers

The problem is that old-style enumerations are unscoped. You can avoid this problem (provided your compiler has the relevant C++11 support) by using scoped enumerations:

enum class GameObjectType { NINJA_PLAYER };  enum class GameObjectTypeLocation { NONE, MASSAGE_ALL, NINJA_PLAYER }; 

Alternatively, you can put your old-school enumerations in namespaces:

namespace foo {   enum GameObjectType { NINJA_PLAYER }; } // namespace foo  namespace bar {   enum GameObjectTypeLocation { NONE, MASSAGE_ALL, NINJA_PLAYER }; } // namespace bar 

Then your enum values will be foo::NINJA_PLAYER, bar::NINJA_PLAYER etc.

like image 100
juanchopanza Avatar answered Sep 25 '22 06:09

juanchopanza


If you have the possibility to use C++11 I would recommend to use enum class feature to avoid collisions:

enum class GameObjectType {      NINJA_PLAYER  }; enum class GameObjectTypeLocation {     NONE,     MASSAGE_ALL,  //this is for ComponentMadiator     NINJA_PLAYER   }; 

Edit: If you do not have this ability, then you will need to use two different namespaces for each enum.

like image 45
Samuel Avatar answered Sep 23 '22 06:09

Samuel