-
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;
break;
case "troll": hp << 120;
break;
case "elf": hp << 90;
break;
case "wizard": hp << 80;
break;
}
-
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.
Try:
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;
}
-
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
-
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.
-
and while we're at it, C++0x now offers strongly-typed enums and scoped enums:
http://www.informit.com/guides/conte...lus&seqNum=316
Danny Kalev
-
 Originally Posted by Danny
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
-
By geo039 in forum ASP.NET
Replies: 3
Last Post: 08-06-2007, 03:46 PM
-
Replies: 1
Last Post: 10-01-2006, 05:08 PM
-
By Dark Rain in forum Java
Replies: 8
Last Post: 09-30-2005, 06:42 PM
-
By VanDam in forum Database
Replies: 0
Last Post: 07-09-2005, 04:18 AM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|