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 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/
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里面的主题定义,所以 ...
随机推荐
- vue中使用动画vue-particles实现背景粒子酷炫效果
先来看我做的效果 我这个是用的背景色加上这个粒子效果实现的demo 平时我们做项目的话会添加背景图片这些,可能更加好看 看我的实现步骤 cnpm install -g vue-cli vue init ...
- Loadrunner学习---脚本编写(1)
Loadrunner学习---脚本编写(1) 中午看了两集<奋斗>发现越看越想看,但是想到好不容易没上班,在家还是赶紧学习下LR的知识吧.下面这个网页的文章原来也是看过的,但发现没几天就忘 ...
- LoadRunner脚本编写(6)— 数据类型转换和字符串操作
LoadRunner脚本编写(6)— 数据类型转换和字符串操作 一,数据类型转换 没有使用过C编程的LoadRunner脚本编写者会发现在数据类型转化方面比较困难.下面介绍这方面的知识. 1. 相似函 ...
- Python 的一些方法
有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象.Pickling). 你可以把它当作一个教程 ...
- 学习笔记css3
边框 盒子圆角 border-radius:5px / 20%: border-radius:5px 4px 3px 2px; 左上,右上,右下,左下 盒子阴影 box-shadow:box-shad ...
- Python-线程(3)-协程
目录 Event事件 线程池 进程池 回调函数 高性能爬取梨视频 协程 yield保存状态 gevent模块 协程的目的 TCP服务端单线程下实现并发 Event事件 event 事件用来控制线程的执 ...
- JVM的内存空间
一.JVM运行起来,就会给内存划分空间,这块空间成为运行时数据区.运行时数据区主要划分为以下几部分内容: 1.栈 每一个线程运行起来的都会对应一个栈(线程栈),栈中的数据是该线程独有的,不会产生资源共 ...
- vuex结合vue-cookies的使用
一.创建vuex import Vue from 'vue' import Vuex from 'vuex' import cookie from "vue-cookies" Vu ...
- Elasticsearch基本命令
检查集群运行情况: GET -> localhost:9200/_cat/health?v 查看集群节点列表: GET -> localhost:9200/_cat?n ...
- php包含文件
PHP 包含文件 PHP include 和 require 语句 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容. include 和 require 语句用于在执 ...