Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

I've been running into a problem and couldn't find any info how to fix this. Maybe one of you has the solution.

typedef enum menuStates
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
};

throws the follwoing warning in ADS - "C2224W: storage-class without decalarator is spurious"

I'm not sure how to fix this because when I add a declarator like this for example

typedef enum menuStates
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
} _menuStates;

I am getting a linker error, because being in a header file the enumaration has now been declared in all my object files, causing duplication problems.

So, does anyone know how to properly use this sort of thing so that the compiler AND the linker are happy?

Don't you have to explicitly set the first enum to 0 or whatever it is you want it to start at?
enum
{
MENU_MAIN = 0,
MENU_WHATEVER,
...
;
That's how I do it and I've never had a problem.

Don't you have to explicitly set the first enum to 0 or whatever it is you want it to start at?
enum
{
MENU_MAIN = 0,
MENU_WHATEVER,
...
;
That's how I do it and I've never had a problem.

0 is the default value for all enumerations so you don't have to define it explicitely.
Things work great without the typedef, just the way you describe it, but if you want to define a datatype for the enumeration the warning shows up - in ADS only, mind you. VS is handling it without any issues, but that's hardly surprising given that compiler's poor typechecking.

0 is the default value for all enumerations so you don't have to define it explicitely.
Things work great without the typedef, just the way you describe it, but if you want to define a datatype for the enumeration the warning shows up - in ADS only, mind you. VS is handling it without any issues, but that's hardly surprising given that compiler's poor typechecking.

What about doing this instead:
typedef enum
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
} menuStates;
/kUfa

What about doing this instead:
typedef enum
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
} menuStates;
/kUfa

Linker error. I tried that before. :-)

Linker error. I tried that before. :-)

Well an enum is not of any kind, so why should we use a typedef on it?
enum menuStates
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
};
Should work fine..
/kUfa

Well an enum is not of any kind, so why should we use a typedef on it?
enum menuStates
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
};
Should work fine..
/kUfa

Enums are indeed of a type, they are intrinsically an integer type. By using typedef on them you are creating a new datatype that can hold ONLY the values that are part of the enumeration, complete with proper compiler type checking. Of course your version works, but it's not doing the same thing. I no longer have the menuStates datatype, and I also no longer have type checking for it, of course.
I am not trying to find an alternative version. I am trying to find out the proper ANSI-compliant way of doing this.
I happen to need the typedef enum datatype for proper typechecking. I am not going to sacrifice my code quality for a compiler warning. I'd rather turn off the warning - but only as the very last alternative - but that's entirely beside the point. The point is that there has to be a correct way of defining and declaring this enumeration type without warnings and I just don't know how it's being done.

Enums are indeed of a type, they are intrinsically an integer type. By using typedef on them you are creating a new datatype that can hold ONLY the values that are part of the enumeration, complete with proper compiler type checking. Of course your version works, but it's not doing the same thing. I no longer have the menuStates datatype, and I also no longer have type checking for it, of course.
I am not trying to find an alternative version. I am trying to find out the proper ANSI-compliant way of doing this.
I happen to need the typedef enum datatype for proper typechecking. I am not going to sacrifice my code quality for a compiler warning. I'd rather turn off the warning - but only as the very last alternative - but that's entirely beside the point. The point is that there has to be a correct way of defining and declaring this enumeration type without warnings and I just don't know how it's being done.

What kind of linker errors are you getting? Does your header file have a proper macro guard to prevent multiple definition?

What kind of linker errors are you getting? Does your header file have a proper macro guard to prevent multiple definition?

Quote:Enums are indeed of a type, they are intrinsically an integer type
Yes they are a type, but NO they aren't integers.
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.18
http://std.dkuug.dk/jtc1/sc22/open/n2356/dcl.html#dcl.enum
Try this c++ code:
enum menuStates
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
};
void test()
{
menuStates toto = MENU_CREDITS; // ok
menuStates toto2 = 3; // error, cannot convert from int to menuStates

So a menuStates can ONLY hold the values that are part of the enumeration, and i guess that's what you try to achieve..
/kUfa

Quote:Enums are indeed of a type, they are intrinsically an integer type
Yes they are a type, but NO they aren't integers.
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.18
http://std.dkuug.dk/jtc1/sc22/open/n2356/dcl.html#dcl.enum
Try this c++ code:
enum menuStates
{
MENU_MAIN, MENU_CREDITS, MENU_HELP, MENU_HISCORE,
MAX_MENUSTATES
};
void test()
{
menuStates toto = MENU_CREDITS; // ok
menuStates toto2 = 3; // error, cannot convert from int to menuStates

So a menuStates can ONLY hold the values that are part of the enumeration, and i guess that's what you try to achieve..
/kUfa

kUfa,
You may be on to something there... let me check that out.

kUfa,
You may be on to something there... let me check that out.

You're absolutely right, kUfa. It works fine without the typedef...
Thanks, man.

You're absolutely right, kUfa. It works fine without the typedef...
Thanks, man.

Np, glad it helped!
/kUfa

Np, glad it helped!
/kUfa