简体   繁体   中英

How to use C++ enums without getting a compiler warning

I need to communicate that one and the same enum is passed to several calls. So I am doing this:

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

myEnergy->Transfer(ally, 10);
myLives->Transfer(ally, 1);

Both Transfer methods are declared as follows:

Energy::Transfer(const Creatures& transferTo, (snip)

However, I am getting the following warning on the declaration of the variable named ally:

 warning C4482: nonstandard extension used: enum 'MiddleEarth::Creatures' used in qualified name

What am I doing wrong? How do I rewrite my code so that it does not generate a compiler warning?

From the MSDN page on the warning ;

When you refer to an enum inside a type, you do not need to specify the name of the enum.

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

so in your case;

MiddleEarth::Creatures::Elf

should be

MiddleEarth::Elf

您可能想要:

MiddleEarth::Creatures ally = MiddleEarth::Elf;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM