如何在不收到编译器警告的情况下使用C++枚举

A-K*_*A-K 1 c++ enums visual-c++

我需要传达一个相同的枚举传递给多个调用.所以我这样做:

MiddleEarth::Creatures ally = MiddleEarth::Creatures::Elf;

myEnergy->Transfer(ally, 10);
myLives->Transfer(ally, 1);
Run Code Online (Sandbox Code Playgroud)

两种Transfer方法都声明如下:

Energy::Transfer(const Creatures& transferTo, (snip)
Run Code Online (Sandbox Code Playgroud)

但是,我对名为ally的变量的声明收到以下警告:

 warning C4482: nonstandard extension used: enum 'MiddleEarth::Creatures' used in qualified name
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如何重写我的代码,以便它不会生成编译器警告?

Joa*_*son 6

MSDN页面上发出警告 ;

当您在类型中引用枚举时,您不需要指定枚举的名称.

int i = S :: E :: a; // C4482
int j = S :: a; // 好

所以在你的情况下;

MiddleEarth::Creatures::Elf
Run Code Online (Sandbox Code Playgroud)

应该

MiddleEarth::Elf
Run Code Online (Sandbox Code Playgroud)