title=VARIABLE cannot appear in a constant-expression
Appearance
Causes
[edit | edit source]Using a variable for a switch case statement
[edit | edit source]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
[edit | edit source]- 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