开发者

Two enums have some elements in common, why does this produce an error?

开发者 https://www.devze.com 2022-12-18 06:37 出处:网络
I have two enums in my code: enum Month {January, February, March, April, May, June, July, August, September, October, November, December};

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 co开发者_Python百科mpiler says:

Redeclaration of enumerator 'May'.

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


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


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, ....


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


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


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?


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消