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

后端 未结 2 492
猫巷女王i
猫巷女王i 2020-12-30 19:32

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

enu         


        
相关标签:
2条回答
  • 2020-12-30 20:13

    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.

    0 讨论(0)
  • 2020-12-30 20:15

    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.

    0 讨论(0)
提交回复
热议问题