为什么 switch-case 语句中不能有变量?
这是我的代码:
bool Character::keyPress(char c)
{
switch(c)
{
case up_key:
move(0, -1);
break;
case down_key:
move(0, 1);
break;
case left_key:
move(-1, 0);
break;
case right_key:
move(1,0);
break;
default:
return false;
}
return true;
}
编译器抱怨:
error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
在我的头文件中,我有:
protected:
char up_key;
char down_key;
char right_key;
char left_key;
我正在使用 Visual C++ 2008。
Here is my code:
bool Character::keyPress(char c)
{
switch(c)
{
case up_key:
move(0, -1);
break;
case down_key:
move(0, 1);
break;
case left_key:
move(-1, 0);
break;
case right_key:
move(1,0);
break;
default:
return false;
}
return true;
}
And the compiler complains:
error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
In my header file I have:
protected:
char up_key;
char down_key;
char right_key;
char left_key;
I am using Visual C++ 2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如错误消息所述,case 表达式必须是常量。编译器在编译时将其构建为一个非常快速的查找表,如果这些值有可能随着程序运行而改变,则它无法做到这一点。
如果您确实需要它们是可变的,而不是恒定的,那么最好的选择是使用 if/else 语句。
As the error message states, the case expressions must be constant. The compiler builds this as a very fast look-up table at compile time and it can't do that if there is a possibility that the values could change as the program runs.
If you do need them to be variable, not constant, your best bet is to use if/else statements instead.
代码替换这个又长又笨拙的代码
用这样的
:您可以用更简洁的单行代码替换这 17 行长的代码。
Replace this long clumsy code,
with something like this:
You can litterly replace that 17 lines long of code with that much more neat single line of code.
你不能,因为语言不是这样工作的。例如,如果
up_key
、down_key
、right_key
和left_key
全部相等,会发生什么情况?You can't because the language doesn't work that way. For example, what would happen if
up_key
,down_key
,right_key
, andleft_key
were all equal?由于
switch
语句只能采用常量,因此您在阅读代码时就知道要比较的内容都是常量。另一方面,您可以使用 if 语句(或其他结构)来与变量进行比较:这提供了结构上的明显差异,可以极大地帮助那些在您之后阅读代码的人。想象一下,如果您必须查找每个
case
标签来查看它是否是变量?Because the
switch
statement can take only constants, you know when reading the code that the things you're comparing against are all constants. On the other hand, you would useif
statements (or some other structure) to compare against variables:This provides a distinct difference in structure which can greatly aid those who come after you in reading your code. Imagine if you had to look up every
case
label to see whether it was a variable or not?我相信这是因为编译器生成了一个跳转表,其中的值被硬编码,尽管我可能是错的。表的生成方式不允许这样做。
I believe it's because the compiler generates a jump table, with the values hardcoded in, although I may be wrong. The way the tables are generated just doesn't allow for it.
由于其他答案已经涵盖了您收到错误的原因,因此这里有一种方法可以在四个方向之一上移动以响应按键:使用查找表而不是条件/开关。
设置部分:
功能:
Since other answers have covered why you are getting an error, here is a way to move in one of the four directions in response to a key press: use lookup tables instead of the conditionals/switches.
Setup portion:
The function: