简体   繁体   English

“ opt”不能出现在常量表达式中

[英]“opt” cannot appear in constant-expression

In the following C++ program: 在以下C ++程序中:

int opt;
in(opt);
switch(opt)
 case(opt == 1):
 //and so on…

where in(opt); 在哪里in(opt); is the procedure used to get the value of the integer opt. 是用于获取整数opt值的过程。

I get the error here: case(opt == 1): 我在这里得到错误: case(opt == 1):

Basically the point is I want to make a way for the user to decide what feature of the program is going to be used. 基本上,关键是我想为用户提供一种方法来决定要使用的程序功能。 I also tried with a char but had no luck with it as well. 我也尝试过使用char,但是也没有运气。 I just can't figure out what's going on. 我只是不知道发生了什么事。

The case labels in a switch block need to be compile time evaluable constant expressions (and integral types). switch块中的case标签需要是编译时可评估的常量表达式(和整数类型)。

Since opt == 1 is only known at run-time, compilation of case (opt == 1) will fail. 由于opt == 1仅在运行时已知,因此case (opt == 1)编译将失败。

Did you mean simply case 1: ? 您是说简单的case 1:吗?

switch(opt) {
    case 1:
        break;
    case 2:
        break;
}

The switch part says that you're looking at the value of opt ; switch部分表示您在看opt的值; each case statement gives a possible matching value. 每个case语句都给出一个可能的匹配值。 The value in a case statement has to be a compile-time constant. case语句中的值必须是编译时常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM