Case expression not constant

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Case expression not constant

Hybrid View

  1. #1
    Join Date
    Feb 2011

    Case expression not constant

    hello, im making a simple rpg and this error kept coming up, and i dont know how to fix it. these are the cases i have.

    switch (job)
    case "human": hp << 100;
    case "troll": hp << 120;
    case "elf": hp << 90;
    case "wizard": hp << 80;

  2. #2
    Join Date
    Oct 2007
    Luckily for you, literal strings don't always convert to constant integers.
    The object of the case statement must be an integral type or something that converts to an integral type.
    enum Species { HUMAN, TROLL, ELF, WIZARD };

    Let "job" be declared:
    Species job;

    Then, you have:
    switch (job)
    case HUMAN: hp << 100; break;
    case TROLL: hp << 120; break;
    case ELF: hp << 90; break;
    case WIZARD: hp << 80; break;

  3. #3
    Join Date
    Nov 2003
    The labels of a case statement must be of an integral constant type. Therefore, you can't use any other constants, including literal strings. The most effective technique to overcome this is by using enum types, as hendrixj showed.
    Danny Kalev

  4. #4
    Join Date
    Dec 2003
    I always add a max to my enums.

    Something like this:

    enum Species { HUMAN, TROLL, ELF, WIZARD, Species_Max };

    so you can do this:

    for(x = human; x < Species_Max; x++)

    instead of this:

    for(x = human; x <= Wizard; x++)

    .... my way, if you add in dwarves later on, nothing else changes (max is still max, its just a different number) whereas if you do it the second way, you have to change ALL the loops and other such references.

    You can also do this nicely
    sometype container[species_max] using the same principles, never change the size, it grows to fit if you add more stuff.

  5. #5
    Join Date
    Nov 2003
    and while we're at it, C++0x now offers strongly-typed enums and scoped enums:
    Danny Kalev

  6. #6
    Join Date
    Dec 2003
    Quote Originally Posted by Danny View Post
    and while we're at it, C++0x now offers strongly-typed enums and scoped enums:
    I would still give a lot to be able to print the enum's name out or get at it as a string.

Similar Threads

  1. Databind Dropdown
    By geo039 in forum ASP.NET
    Replies: 3
    Last Post: 08-06-2007, 04:46 PM
  2. errors with some varibles
    By ELHEK in forum Java
    Replies: 1
    Last Post: 10-01-2006, 06:08 PM
  3. Why is this in the console?
    By Dark Rain in forum Java
    Replies: 8
    Last Post: 09-30-2005, 07:42 PM
  4. Run-time error by a blank record made by code
    By VanDam in forum Database
    Replies: 0
    Last Post: 07-09-2005, 05:18 AM
  5. ADOX problem
    By Yoel Martinez in forum VB Classic
    Replies: 1
    Last Post: 12-12-2000, 04:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center