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里面的主题定义,所以 ...
随机推荐
- js实现把多个数据分成3个一组
主要代码: var stuCount = res.data; if (stuCount.length>0){ var objList = new Object(); var arr = new ...
- layui实现批量导入excal表
layui实现多文件上传,并直接选中需要上传文件的类型 //拖拽上传 upload.render({ elem : '#import', size: , //限制文件大小,单位 KB accept: ...
- Ubuntu 18.04 Linux上安装Etherpad,基于Web的实时协作编辑器
介绍 Etherpad是一个开源的,基于Web的实时协作编辑器(http://www.0834nanke.com) 它允许多个人使用他们的Web浏览器同时编辑文档. 它还提供了一些很酷的功能,如富文本 ...
- Java驼峰和下划线互相转化
直接上代码 : package com.utils; public class ChangeChar { public static final char UNDERLINE = '_'; publi ...
- 第十四章 Odoo 12开发之部署和维护生产实例
本文中将学习将 Odoo 服务器作为生产环境的基本准备.安装和维护服务器是一个复杂的话题,应该由专业人员完成.本文中所学习的不足以保证普通用户创建应对包含敏感数据和服务的健壮.安全环境. 本文旨在介绍 ...
- Docker这个新软件究竟是用来干嘛的???
http://dockone.io/article/378 尝试新软件 对开发者而言,每天会催生出的各式各样的新技术都需要尝试,然而开发者却不太可能为他们一一搭建好环境并进行测试.时间非常宝贵,正是得 ...
- Windows的SEH机理简要介绍
1.异常分类 一般来说,我们把Exception分为2类,一类是CPU产生的异常,我们称之为CPU异常(或者硬件异常).另一类为是通过调用RaiseException API产生的软件异常,我们称之为 ...
- Android之LinearLayout线性布局
1.相关术语解释 orientation 子控件的排列方式,horizontal(水平)和vertical(垂直默认)两种方式! gravity 子控件的对齐方式,多个组合 layout_gravit ...
- sqlserver2005分页存储过程
Create proc [dbo].[sp_AbiTableLoad] ---------------------------------------------- -- 单表多表分页存储过程 -- ...
- python-web-习题
1.简单描述 webbrowser.requests.BeautifulSoup 和 selenium 模块之间的不同 webbrowser模块有一个 open() 方法,它启动 web 浏览器,打开 ...