case expressions must be constant expressions
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 case
statements require integral value which must be known at compile-time, which is what is meant by constant here. But the const
members 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. const
in C++ means read-only, and a const variable can be initialized just like int y = 0; const int x = y;
, making x
a read-only copy of the value y
had at the time of initialization.
With a modern compiler, you can either use enum
s or constexpr
s 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/
case expressions must be constant expressions的更多相关文章
- android switch语句报错:case expressions must be constant expressions
今天无意中碰见了 case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...
- android switch语句case expressions must be constant expressions
在项目中遇到这样的Exception:case expressions must be constant expressions public class StandingCityActivity e ...
- Android switch 中 case expressions must be constant expressions 错误
刚才导入android zxing 条码 的demo测试,发现出现如下错误 case expressions must be constant expressions 经检查,项目被设置成librar ...
- (转)android import library switch语句报错case expressions must be constant expressions
今天当我从github上下载一个工程,并把它的库文件导入eclipse中,发现switch语句报错case expressions must be constant expressions : 解决方 ...
- Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions
原文地址:http://blog.csdn.net/wchinaw/article/details/7325641 在一般的Android项目里R里面的资源声明看起来是这样的: public stat ...
- C11 constant expressions 常量表达式
一图流总结hhh
- Android Library Project 使用问题总结
1. 当新建Android Library Project 工程或将已有工程转化为Android Library Project, 如果工程源代码中有如下语句: int id = view.getId ...
- [改善Java代码]小心switch带来的空值异常
使用枚举定义常量时,会伴有大量的switch语句判断,目的是伪类每个枚举项解释其行为,例如: public class Client { public static void main(String[ ...
- SlidingMenu源代码导入及错误分析和解决方法
1.首先下载actionbarsherlock和SlidingMenu源代码 由于在SlidingMenu项目中,styles.xml文件使用到了actionbarsherlock里面的主题定义,所以 ...
随机推荐
- Python全栈开发:XML与parse对比
#!/usr/bin/env python # -*- coding;utf-8 -*- """ ET.XML和ET.parse的对比 1.返回对象差异: ET.XML: ...
- Powerdesigner 生成数据字典
https://www.jianshu.com/p/f491d0d3c503http://blog.csdn.net/adparking/article/details/50402980http:// ...
- SG函数模板(洛谷2197nim游戏
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> ...
- python事件调度库sched
事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接 ...
- Android之LinearLayout线性布局
1.相关术语解释 orientation 子控件的排列方式,horizontal(水平)和vertical(垂直默认)两种方式! gravity 子控件的对齐方式,多个组合 layout_gravit ...
- python数据类型,数据结构
数据类型:int,bool 数据结构:dict,list,tuple,set,str
- [Ynoi2012]D1T1
题目 套路的根号分治啊 我们设置一个值\(S\) 对于\(S\leq x\)的操作,我们直接暴力修改,显然这样只会修改\(\frac{n}{S}\)次,所以我们需要一个能够\(O(1)\)修改的数据结 ...
- Less主要用法
一.混合(Mixin) 原css中的样式如: .header { width:200px; height:100px; } .header .word{ color:red; } less中的写法可以 ...
- mybatis-输入输出映射-动态sql-关联查询-mybatis与spring-逆向工程
1.1. 输入映射和输出映射 Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 1.1.1. 环境准备 1. 复制昨天的工程 ...
- 备份和恢复MySQL数据库
一.备份 1) 备份表mysqldump -uroot -p 库名 表1 > e:\backup.sqlmysqldump -uroot -p 库名 表1 表2 表3 > e:\backu ...