case expressions must be constant expressions
2017-04-11 17:42
447 查看
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.
方法一:
The
Instead of fields, you can use
And then you can write,
I am getting a 'case expression not constant' error in a switch statement. However, the header provides a definition for the used constants, and the constructor provides initialisation for them in its initialization list.
Additionally, when I mouse over the "problem" statements it identifies them as constants.
This seems a little counter-intuitive to me. I did some research and found a similar problem that someone else had. They were told that all constants must in fact be initialised in 'main' and that this was a limitation of the language. Is this really the case? It seems unlikely.
You need a "real" compile time integer constant.
With a modern compiler, you can either use
其它: http://www.cplusplus.com/forum/beginner/74845/
If you do need them to be variable, not constant, your best bet is to use if/else statements instead.
方法一:
The
casestatements require integral value which must be known at compile-time, which is what is meant by constant here. But the
constmembers of a class are not really constant in that sense. They're are simply read-only.
Instead of fields, you can use
enum:
class ThisClass { public: enum Constants { EXAMPLE_CONSTANT = 10, ANOTHER_CONSTANT = 20 }; };
And then you can write,
switch(c) { case ThisClass::EXAMPLE_CONSTANT: //code break; case ThisClass::ANOTHER_CONSTANT: //code break; };
I am getting a 'case expression not constant' error in a switch statement. However, the header provides a definition for the used constants, and the constructor provides initialisation for them in its initialization list.
Additionally, when I mouse over the "problem" statements it identifies them as constants.
const int ThisClass::EXAMPLE_CONSTANT error expression must have a constant value
This seems a little counter-intuitive to me. I did some research and found a similar problem that someone else had. They were told that all constants must in fact be initialised in 'main' and that this was a limitation of the language. Is this really the case? It seems unlikely.
You need a "real" compile time integer constant.
constin C++ means read-only, and a const variable can be initialized just like
int y = 0; const int x = y;, making
xa read-only copy of the value
yhad at the time of initialization.
With a modern compiler, you can either use
enums or
constexprs to store (integral) members of compile-time-constness:
class Foo { public: static constexpr int x = 0; enum { y = 1 }; }; int main () { switch (0) { case Foo::x: ; case Foo::y: ; } }
其它: http://www.cplusplus.com/forum/beginner/74845/
相关文章推荐
- Error处理:Java switch case 报错:case expressions must be constant expressions
- case expressions must be constant expressions
- Android 出现case expressions must be constant expressions
- case expressions must be constant expressions
- Zxing库遇到case expressions must be constant expressions问题的解决办法
- 举例说明出现case expressions must be constant expressions错误的原因及其解决办法
- Android的Switch语句case expressions must be constant expressions错误的解决方法
- case expressions must be constant expressions
- Zxing库遇到case expressions must be constant expressions问题的解决办法
- case expressions must be constant expressions
- case expressions must be constant expressions
- java switch 异常 case expressions must be constant expressions
- Android 出现case expressions must be constant expressions
- Zxing库遇到case expressions must be constant expressions问题的解决办法
- android switch语句case expressions must be constant expressions
- case expressions must be constant expressions
- Zxing库遇到case expressions must be constant expressions问题的解决办法
- android switch语句case expressions must be constant expressions
- Android 库工程 提示case expressions must be constant expressions 错误
- android switch语句报错:case expressions must be constant expressions