411 REFERENCE · Wikibooks
GCC Debugging/g++/Errors/variable cannot appear in a constant-expression
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Causes
Using a variable for a switch case statement
While a switch statement often use a variable for an argument, their case statements cannot use variables as arguments.
int foo = 7;
int bar = 4;
switch(foo) {
case bar:
cout << "This is case 0" << endl;
}
Solution: Make the case statement's argument a constant
int foo = 7;
const int bar = 4;
switch(foo) {
case bar:
cout << "This is case 0" << endl;
}
Notes
Message found in GCC version 4.5.1
In GCC version 3.2.3, reported as: case label does not reduce to an integer constant
This message can come up about strings- however, you cannot make a string constant