468,512 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

enum value not handled in switch

32
This post went around back in January. I need some clarification.

I have an enum declaring 30 enumerated values

a=0
b=1
c=2
etc

I have a switch statement that I don't want to use all 30 enumerations in.

I get the compiler error "enumeration value not handled in switch"
I am using g++ on a unix box.

The -Wswitch compiler option that was suggested in the previous posting is available to me but it does not supress the warning. Using it does not suppress the error.
Actually the previous post said -Wswitch-enum which I don't see in the gnu manpage.

What is the correct option to use -Wswitch or -Wswitch-enum? and where does it go in the makefile. In CFLAGS or LDFLAGS???

The default case does stop the warnings, but, if the compiler option is doing what it's supposed to do, I shouldn't need the default case.

Thanks

emp1953
Nov 29 '07 #1
1 15064
weaknessforcats
9,207 Expert Mod 8TB
An enum is not a list if integer variables. It is a list of names for integer values so you can avoid hard-coded values in your program.

Therefore, you cannot switch on an enum.

In C++, you have to create a variable and switch on that:
Expand|Select|Wrap|Line Numbers
  1. enum Value {A,B};
  2.  
  3. int main()
  4. {
  5.     Value arg = A;
  6.     switch (arg)
  7.     {
  8.        case A:
  9.            break;
  10.  
  11.         default:
  12.             break;
  13.     }
  14. }
  15.  
C is different. It doesn't see Value as a type. Here you have to typedef:
Expand|Select|Wrap|Line Numbers
  1. typedef enum {A,B} Value;
  2.  
  3. int main()
  4. {
  5.     Value arg = A;
  6.     switch (arg)
  7.     {
  8.        case A:
  9.            break;
  10.  
  11.         default:
  12.             break;
  13.     }
  14. }
  15.  
Nov 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by AngleWyrm | last post: by
8 posts views Thread by Imran | last post: by
5 posts views Thread by DJTB | last post: by
9 posts views Thread by ccwork | last post: by
21 posts views Thread by Andreas Huber | last post: by
13 posts views Thread by Adam Blair | last post: by
34 posts views Thread by Steven Nagy | last post: by
8 posts views Thread by Mark P | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.