Reputation: 73
I was using this code as part of a 2 dimensional game, but when i tried compiling it, it returned the following error:
error C2051: case expression not constant.
This is my code:
switch(_getch()){
case "w":
dir = UP;
break;
case "a":
dir = LEFT;
break;
case "s":
dir = DOWN;
break;
case "d":
dir = RIGHT;
break;
default:
break;
}
Upvotes: 1
Views: 225
Reputation: 16431
You should use character literals ('w'
) instead of string literals ("w"
) in switch cases:
case 'w':
dir = UP;
break;
"w"
is a string literal, which would decay to a char const*
pointer. switch
cases cannot be anything other than a constant integer, an enum or a class with a single non-explicit integer or enum conversion operator. A pointer to char is none of those.
Upvotes: 1