我的代码中有两个枚举:
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
是两个枚举中的公共(public)元素,因此编译器说:
Redeclaration of enumerator '
May
'.
为什么这么说?我该如何避免这种情况?
最佳答案
枚举名称在全局范围内,它们需要是唯一的。请记住,您不需要使用枚举名称来限定枚举符号,您只需:
Month xmas = December;
不是:
Month xmas = Month.December; /* This is not C. */
出于这个原因,您经常会看到人们在符号名称前加上枚举的名称:
enum Month { Month_January, Month_February, /* and so on */ };
关于c++ - 两个枚举有一些共同的元素,为什么会产生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2161940/