在类型中使用枚举 - 编译器警告 C4482 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/514194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using enum inside types - Compiler warning C4482 C++
提问by Navaneeth K N
I am using fully qualified name of the enum inside a method in one of my class. But I am getting compiler warning which says "warning C4482: nonstandard extension used: enum 'Foo' used in qualified name". In C++, do we need to use enums without the qualified name? But IMO, that looks ugly.
我在我的一个类的方法中使用枚举的完全限定名称。但是我收到了编译器警告,上面写着“警告 C4482:使用了非标准扩展:enum 'Foo' used inqualified name”。在 C++ 中,我们是否需要使用没有限定名称的枚举?但是 IMO,那看起来很难看。
Any thoughts?
有什么想法吗?
采纳答案by sth
Yes, enums don't create a new "namespace", the values in the enum are directly available in the surrounding scope. So you get:
是的,枚举不会创建新的“命名空间”,枚举中的值在周围范围内直接可用。所以你得到:
enum sample {
SAMPLE_ONE = 1,
SAMPLE_TWO = 2
};
int main() {
std::cout << "one = " << SAMPLE_ONE << std::endl;
return 0;
}
回答by Poy
To make it clean, replace:
要使其清洁,请替换:
enum Fruit {
ORANGE = 0,
BANANA = 1
};
with
和
namespace Fruit {
enum { //no enum name needed
ORANGE = 0,
BANANA = 1
};
};
...
int f = Fruit::BANANA; //No warning
回答by jmc
While sth does answer the question, it didn't address how I've always used enums. Even though they're just more or less names for numbers, I've always used them to define types that can only have certain values.
虽然 sth 确实回答了这个问题,但它没有解决我一直使用枚举的方式。尽管它们或多或少只是数字的名称,但我总是使用它们来定义只能具有某些值的类型。
If the enum is part of the class, then that helps consumers clearly identify an enum reference:
如果枚举是类的一部分,那么这有助于消费者清楚地识别枚举引用:
class Apple {
enum Variety {
Gala,
GoldenDelicious,
GrannySmith,
Fuji
}
...
};
Then consumers would be able declare instances of the enum, pass as parameters, and qualify them when referencing one of the types.
然后消费者将能够声明枚举的实例,作为参数传递,并在引用其中一种类型时限定它们。
unsigned int GetCountOfApples( Apple::Variety appleVariety );
...
fujiCnt = GetCountOfApples( Apple::Fuji );
Sometimes you want an enum outside of a class or two enums in the same class, and you can do something like what Poy had. You won't be able to reference the enum type though, so just name it.
有时你想要一个类之外的枚举或同一个类中的两个枚举,你可以做一些类似于 Poy 的事情。不过,您将无法引用枚举类型,因此只需命名即可。
namespace Color {
enum ColorEnum {
Blue,
Red,
Black
};
Now using the enum and values would work like:
现在使用枚举和值的工作方式如下:
Color::ColorEnum firstColor = Color::Blue;
Color::ColorEnum secondColor = Color::Red;
if( firstColor == secondColor )
....
Now if there happens to be different enums with the same name in them, they will always be qualified with what type they are. Then you could handle what gamblor is asking about.
现在,如果碰巧有不同的枚举在它们中具有相同的名称,它们将始终以它们的类型进行限定。然后你可以处理赌徒所问的问题。
BananaColorEnum banCol = BananaColor::Yellow;
TomatoColorEnum tomCol = TomatoColor::Yellow;
回答by Rob K
Yes. Conceptually enum defines a type, and the possible values of that type. Even though it seems natural, to define enum foo { bar, baz };
and then refer to foo::baz
is the same as referring to int::1
.
是的。从概念上讲,枚举定义了一个类型,以及该类型的可能值。尽管看起来很自然,但定义enum foo { bar, baz };
然后引用foo::baz
与引用int::1
.
回答by Tomas Kubes
namespace Company
{
typedef int Value;
enum
{
Microsoft= 0,
APPLE = 1,
};
};
namespace Fruit
{
typedef int Value;
enum
{
ORANGE = 0,
BANANA = 1,
APPLE = 2,
};
};
...
Fruit::Value f = Fruit::BANANA; //No warning
Company::Value f = Company::APPLE; //is different value then Fruit::APPLE
This works on GCC and MS compiler and Mac. And the advantage is that you can use namespace operator and pass conflicts. The little disadvantage is that instead of Fruit, you have to write Fruit::Value. it is more useful in large project when you don't know what enums are in other class.
这适用于 GCC 和 MS 编译器以及 Mac。优点是您可以使用命名空间运算符并传递冲突。一个小缺点是你必须写 Fruit::Value 而不是 Fruit。当您不知道其他类中的枚举是什么时,它在大型项目中更有用。
If it is possible to use C++11 instead, it is much more simple, because the enum::namespace syntax is then possible.
如果可以改用 C++11,那就简单多了,因为 enum::namespace 语法是可能的。
回答by petejamd
The cleanest way I've found to do this is defining the enum as such
我发现这样做的最干净的方法是定义枚举
namespace Samples
{
enum Value
{
Sample1,
Sample2,
Sample3
};
}
typedef Samples::Value Sample;
Then in function and variable definitions you can use the typedef:
然后在函数和变量定义中,您可以使用 typedef:
void Function(Sample eSample);
Sample m_eSample;
And in your .cpp file you can use the namespace to assign variables:
在您的 .cpp 文件中,您可以使用命名空间来分配变量:
void Function(Sample eSample)
{
m_eSample = Samples::Sample1;
eSample = Samples::Sample2;
}
回答by georgejo
Personally, I think this is a compiler bug. I've been using C++ for lots of time. Sadly, no sample code in OP. The interpretation of an enum by the Java people was actually correct iMO. Mine, was like this ...
就个人而言,我认为这是一个编译器错误。我一直在使用 C++ 很多时间。遗憾的是,OP 中没有示例代码。Java 人对枚举的解释实际上是正确的 iMO。我的,是这样的...
class Foo {
enum tMyEnum { eFirstVal = 0, eSecondVal = 1};
// ...
tMyEnum m_myVal;
};
void Foo::MyMethod() {
if(m_myVal == tMyEnum::eFirstVal) {
// ...
}
}
I also tried, Foo::tMyEnum::eFirstVal. Without the qualifiers, everything compiled.
我也试过,Foo::tMyEnum::eFirstVal。没有限定符,一切都编译好了。
回答by SparkyNZ
I had the same problem and I'm not using C++ 11 yet. I much prefer fully qualified namespaces myself too.
我遇到了同样的问题,我还没有使用 C++ 11。我自己也更喜欢完全限定的命名空间。
I disabled this particular warning. I'm sure people will dislike the idea but some may be thankful..
我禁用了这个特殊的警告。我敢肯定人们会不喜欢这个想法,但有些人可能会很感激..
#pragma warning( disable : 4482 )