Case Expression Not Constant

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Case Expression Not Constant

 
0
  #1
Nov 2nd, 2009
When I try to compile this:

  1. void *BaseEnt::GetVar(char const *varname)
  2. {
  3. char name = *varname;
  4. switch(name)
  5. {
  6. case "id":
  7. return (void*)id;
  8. case "name":
  9. return (void*)name;
  10. case "count":
  11. return (void*)count;
  12. default:
  13. return (void*)NULL;
  14. }
  15. }

I get these errors:

  1. ------ Build started: Project: Test Classes, Configuration: Debug Win32 ------
  2. Compiling...
  3. classes.cpp
  4. c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(25) : error C2051: case expression not constant
  5. c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(27) : error C2051: case expression not constant
  6. c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(29) : error C2051: case expression not constant
  7. c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(33) : warning C4065: switch statement contains 'default' but no 'case' labels
  8. Build log was saved at "file://c:\Documents and Settings\tom\My Documents\Visual Studio 2008\Projects\Test Classes\Test Classes\Debug\BuildLog.htm"
  9. Test Classes - 3 error(s), 1 warning(s)
  10. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I thought that things like "id" were constant because they're a litteral?

Any help would be appreciated.

PS. If you need more information on my problem, feel free to ask.
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 357
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 55
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
Nov 2nd, 2009
the param to a switch cannot be a string.
It can only be an integer. To achieve what u are trying use if-else statement alongwith strcmp().
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: slackingjuggalo is an unknown quantity at this point 
Solved Threads: 0
slackingjuggalo slackingjuggalo is offline Offline
Newbie Poster
 
-2
  #3
Nov 2nd, 2009
try this


  1. void *BaseEnt::GetVar(char const *varname)
  2. {
  3. char name = *varname;
  4. switch(name)
  5. {
  6. case 'id': //use ' not "
  7. return (void*)id;
  8. case 'name': //use ' not "
  9. return (void*)name;
  10. case 'count': //use ' not "
  11. return (void*)count;
  12. default:
  13. return (void*)NULL;
  14. }
  15. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC