我有包含在所有文件中的配置文件,我有不同的枚举,但在每个枚举中有相同的元素名称,例如: config.h
enum GameObjectType
{
NINJA_PLAYER
};
enum GameObjectTypeLocation
{
NONE,
MASSAGE_ALL, //this is for ComponentMadiator
NINJA_PLAYER
};但是,当我尝试通过使用正确的枚举名称调用枚举来编译项目时
m_pNinjaPlayer = (NinjaPlayer*)GameFactory::Instance().getGameObj(GameObjectType::NINJA_PLAYER);
ComponentMadiator::Instance().Register(GameObjectTypeLocation::NINJA_PLAYER,m_pNinjaPlayer);我收到编译错误:
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'如何在config.h中保留多个具有不同名称但具有相同元素名称的枚举?
发布于 2014-04-25 17:17:32
问题是老式的枚举没有作用域。您可以通过使用作用域枚举来避免此问题(前提是您的编译器具有相关的C++11支持):
enum class GameObjectType { NINJA_PLAYER };
enum class GameObjectTypeLocation { NONE, MASSAGE_ALL, NINJA_PLAYER };或者,您可以将您的老式枚举放在名称空间中:
namespace foo
{
enum GameObjectType { NINJA_PLAYER };
} // namespace foo
namespace bar
{
enum GameObjectTypeLocation { NONE, MASSAGE_ALL, NINJA_PLAYER };
} // namespace bar那么您的枚举值将是foo::NINJA_PLAYER、bar::NINJA_PLAYER等。
发布于 2014-04-25 17:17:01
如果您有可能使用C++11,我建议您使用枚举类功能来避免冲突:
enum class GameObjectType
{
NINJA_PLAYER
};
enum class GameObjectTypeLocation
{
NONE,
MASSAGE_ALL, //this is for ComponentMadiator
NINJA_PLAYER
};编辑:如果您不具备此功能,则需要为每个枚举使用两个不同的名称空间。
https://stackoverflow.com/questions/23288934
复制相似问题