944,146 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2734
  • C++ RSS
Nov 2nd, 2009
0

Case Expression Not Constant

Expand Post »
When I try to compile this:

c++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Nov 2nd, 2009
0
Re: Case Expression Not Constant
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().
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Nov 2nd, 2009
-2
Re: Case Expression Not Constant
try this


c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 8
Solved Threads: 0
Newbie Poster
slackingjuggalo is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Why is C++ so big?
Next Thread in C++ Forum Timeline: LPD3DXMESH vs ID3DXMesh





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC