C++ 两个枚举有一些共同的元素,为什么会产生错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2161940/
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
Two enums have some elements in common, why does this produce an error?
提问by Pieter
I have two enums in my code:
我的代码中有两个枚举:
enum Month {January, February, March, April, May, June, July,
August, September, October, November, December};
enum ShortMonth {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
May
is a common element in both enums, so the compiler says:
May
是两个枚举中的公共元素,因此编译器说:
Redeclaration of enumerator '
May
'.
枚举器 '
May
' 的重新声明。
Why does it say so? And how can I circumvent this?
为什么这么说?我怎样才能规避这一点?
回答by unwind
Enum names are in global scope, they need to be unique. Remember that you don't need to qualify the enum symbols with the enum name, you do just:
枚举名称在全局范围内,它们需要是唯一的。请记住,您不需要使用枚举名称限定枚举符号,您只需:
Month xmas = December;
not:
不是:
Month xmas = Month.December; /* This is not C. */
For this reason, you often see people prefixing the symbol names with the enum's name:
出于这个原因,你经常看到人们用枚举的名称作为符号名称的前缀:
enum Month { Month_January, Month_February, /* and so on */ };
回答by Alex Brown
I suggest you merge the two:
我建议您将两者合并:
enum Month {
Jan, January=Jan, Feb, February=Feb, Mar, March=Mar,
Apr, April=Apr, May, Jun, June=Jun,
Jul, July=Jul, Aug, August=Aug, Sep, September=Sep,
Oct, October=Oct, Nov, November=Nov, Dec, December=Dec};
Which will have exactly the same effect, and is more convenient.
这将具有完全相同的效果,并且更方便。
If you want January to have the value 1, instead of 0, add this:
如果您希望一月的值为 1 而不是 0,请添加以下内容:
enum Month {
Jan=1, January=Jan, Feb, February=Feb, ....
回答by Alexander Poluektov
In C++, to avoid name clashing you could wrap your enums into structs:
在 C++ 中,为了避免名称冲突,您可以将枚举包装到结构中:
struct Month { enum {January, February, March, April, May, June, July,
August, September, October, November, December}; };
struct ShortMonth { enum {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; };
回答by Alexander Poluektov
What unwind said. But I'd also like to say that your example seems like a pretty unusual use of enums. I can't see the value of having a ShortMonth and a LongMonth both referring to the same thing - this would make sense for strings, but not for enums. Why not just have a single Month enum type?
什么放松说。但我还想说,您的示例似乎非常不寻常地使用枚举。我看不出有一个 ShortMonth 和一个 LongMonth 都指同一个东西的价值 - 这对字符串有意义,但对枚举没有意义。为什么不只有一个 Month 枚举类型?
回答by NathanOliver
In C++11 you can use scoped enumerationsto fix this this. This will remove the names from the global scope and scope them to the enum name.
在 C++11 中,您可以使用作用域枚举来解决这个问题。这将从全局范围中删除名称并将它们范围限定为枚举名称。
enum class Identity
{
UNKNOWN = 1,
CHECKED = 2,
UNCHECKED =3
};
enum class Status
{
UNKNOWN = 0,
PENDING = 1,
APPROVED = 2,
UNAPPROVED =3
};
int main ()
{
Identity::UNKNOWN;
Status::UNKNOW;
}
回答by fortran
My suggestion here is to have just one enum, as they are the same type. If you want short aliases to type less in your code (even if I wouldn't advise you to do so), you can do:
我的建议是只使用一个枚举,因为它们是相同的类型。如果您希望短别名在您的代码中输入更少(即使我不建议您这样做),您可以这样做:
enum Month {
January, Jan = January,
February, Feb = February,
March, Mar = March,
April, Apr = April
May,
June, Jun = June,
July, Jul = July,
...};
And to have different presentation names (short and long), you should have two distinct string arrays that are indexed by the enum.
为了有不同的表示名称(短和长),你应该有两个不同的字符串数组,它们由枚举索引。
char[MAX_MONTH_NAME_LENGTH][12] month_long_names = {
"January", "February", ...
}
char[3][12] short_long_names = {
"Jan", "Feb", ...
}
printf("month %d long name is %s, and short name is %s\n", May, long_month_names[May], short_month_names[May]);
回答by Morfildur
In C enums are used without any type prefix, so you write:
在 C 中使用枚举没有任何类型前缀,所以你写:
month[0] = January;
month[4] = May;
The enum Month and ShortMonth have the same scope so the compiler can't know which May to use. An obvious fix would be to prefix the enums but i'm not sure that your use of these enums in this case is warranted.
枚举 Month 和 ShortMonth 具有相同的范围,因此编译器无法知道使用哪个 May。一个明显的解决方法是为枚举添加前缀,但我不确定在这种情况下您是否有必要使用这些枚举。
回答by cocoa
typedef enum {Jan, January, Feb, February, Mar, March, Apr, April, May, Jun, June, Jul, July,
Aug, August, Sep, September, Oct, October, Nov, November, Dec, December} Month,ShortMonth;
Merge them become one
将它们合二为一