c++ how to have same enum members name in different enum names without getting err:redefinition; previous definition was 'enumerator' -
i have config file include in files there have different enums inside each enum there same element names example: config.h
enum gameobjecttype { ninja_player }; enum gameobjecttypelocation { none, massage_all, //this componentmadiator ninja_player };
but when try compile project calling enums proper enum name
m_pninjaplayer = (ninjaplayer*)gamefactory::instance().getgameobj(gameobjecttype::ninja_player); componentmadiator::instance().register(gameobjecttypelocation::ninja_player,m_pninjaplayer);
i getting compilation error:
error c2365: 'ninja_player' : redefinition; previous definition '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 keep in config.h several enums different names same elements names ?
the problem old-style enumerations unscoped. can avoid problem (provided compiler has relevant c++11 support) using scoped enumerations:
enum class gameobjecttype { ninja_player }; enum class gameobjecttypelocation { none, massage_all, ninja_player };
alternatively, can put 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 enum values foo::ninja_player
, bar::ninja_player
etc.
Comments
Post a Comment