两个枚举有一些共同元素,为什么会产生错误?

发布于 2024-08-19 23:06:13 字数 389 浏览 4 评论 0原文

我的代码中有两个枚举:

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 是两个枚举中的公共元素,因此编译器表示:

重新声明枚举器“May

为什么这么说呢?我该如何规避这个问题?

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:

Redeclaration of enumerator 'May'.

Why does it say so? And how can I circumvent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雨后咖啡店 2024-08-26 23:06:13

枚举名称在全局范围内,它们需要是唯一的。请记住,您不需要使用枚举名称来限定枚举符号,您只需:而

Month xmas = December;

不是:

Month xmas = Month.December;  /* This is not C. */

因此,您经常会看到人们在符号名称前添加枚举名称作为前缀:

enum Month { Month_January, Month_February, /* and so on */ };

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 */ };
独孤求败 2024-08-26 23:06:13

我建议你将两者合并:

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};

效果完全相同,而且更方便。

如果您希望一月的值为 1 而不是 0,请添加以下内容:

enum Month {
  Jan=1, January=Jan, Feb, February=Feb, ....

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:

enum Month {
  Jan=1, January=Jan, Feb, February=Feb, ....
甜妞爱困 2024-08-26 23:06:13

在 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}; };

In C++, to avoid name clashing you could wrap your enums into structs:

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}; };
独孤求败 2024-08-26 23:06:13

在 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;
}

实例

In C++11 you can use scoped enumerations to fix this this. This will remove the names from the global scope and scope them to the enum name.

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;
}

Live Example

娇女薄笑 2024-08-26 23:06:13

放松说什么。但我还想说,您的示例似乎是枚举的非常不寻常的使用。我看不出 ShortMonth 和 LongMonth 都引用同一事物的价值 - 这对于字符串有意义,但对于枚举则不然。为什么不只有一个 Month 枚举类型呢?

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?

莫相离 2024-08-26 23:06:13

我的建议是只使用一个枚举,因为它们是同一类型。如果您希望短别名在代码中少输入(即使我不建议您这样做),您可以这样做:

enum Month {
 January, Jan = January,
 February, Feb = February,
 March, Mar = March,
 April, Apr = April
 May,
 June, Jun = June,
 July, Jul = July,
 ...};

并且要具有不同的表示名称(短和长),您应该有两个不同的字符串数组,它们是由枚举索引。

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]);

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]);
拿命拼未来 2024-08-26 23:06:13

在 C 中,枚举的使用没有任何类型前缀,因此您可以这样写:

month[0] = January;  
month[4] = May;

枚举 Month 和 ShortMonth 具有相同的作用域,因此编译器无法知道要使用哪个 May。一个明显的解决方法是为枚举添加前缀,但我不确定在这种情况下使用这些枚举是否合理。

In C enums are used without any type prefix, so you write:

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.

奢华的一滴泪 2024-08-26 23:06:13
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;

将它们合并为一

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文