tags:

views:

179

answers:

6
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 `mei'. Why? And how can I circumvent this?

+9  A: 

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 */ };
unwind
still, is there a scenario where the two names could conflict? Like, can they be assigned to or compared with enum of their own type? Or is it just compiler writers being protective (or lazy)?
SF.
To have both definitions possible, you'd need overloading resolution taking into account the context of use. There is no (user level) overloading in C. And in C++, overloading resolution doesn't take the context of use into account (excepted for implicit conversion)
AProgrammer
@SF: Imagine two enums: `Colors` and `Fruits`. What would be the value of `Orange`?
mouviciel
An enum has int type in C, it's not a separate type. Some compilers will warn if you compare it with other enums though.
nos
+5  A: 

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?

anon
+1: This is a bad use of enums.
S.Lott
A: 

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.

dbemerlin
+2  A: 

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, ....
Alex Brown
+1, you were faster than me xD
fortran
A: 

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}; };
Alexander Poluektov
I tried this - it doesn't work in C, only c++.
Alex Brown
Yes, but topic-starter's problem appears in C++, too. So I think it could be useful for C++-people as well. Post edited to reflect this change.
Alexander Poluektov
+1  A: 

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