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的更多相关文章

  1. android switch语句报错:case expressions must be constant expressions

    今天无意中碰见了   case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...

  2. android switch语句case expressions must be constant expressions

    在项目中遇到这样的Exception:case expressions must be constant expressions public class StandingCityActivity e ...

  3. Android switch 中 case expressions must be constant expressions 错误

    刚才导入android zxing 条码 的demo测试,发现出现如下错误 case expressions must be constant expressions 经检查,项目被设置成librar ...

  4. (转)android import library switch语句报错case expressions must be constant expressions

    今天当我从github上下载一个工程,并把它的库文件导入eclipse中,发现switch语句报错case expressions must be constant expressions : 解决方 ...

  5. Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions

    原文地址:http://blog.csdn.net/wchinaw/article/details/7325641 在一般的Android项目里R里面的资源声明看起来是这样的: public stat ...

  6. C11 constant expressions 常量表达式

    一图流总结hhh

  7. Android Library Project 使用问题总结

    1. 当新建Android Library Project 工程或将已有工程转化为Android Library Project, 如果工程源代码中有如下语句: int id = view.getId ...

  8. [改善Java代码]小心switch带来的空值异常

    使用枚举定义常量时,会伴有大量的switch语句判断,目的是伪类每个枚举项解释其行为,例如: public class Client { public static void main(String[ ...

  9. SlidingMenu源代码导入及错误分析和解决方法

    1.首先下载actionbarsherlock和SlidingMenu源代码 由于在SlidingMenu项目中,styles.xml文件使用到了actionbarsherlock里面的主题定义,所以 ...

随机推荐

  1. 2.初始化spark

    参考:  RDD programming guide http://spark.apache.org/docs/latest/rdd-programming-guide.html  SQL progr ...

  2. linux 解压 WinRAR 压缩文件

    1.Download rar for linux wget http://www.rarlab.com/rar/rarlinux-x64-5.5.b1.tar.gz 2.Configure rar t ...

  3. about how to determine a prime number

    (1) if divided by 2 or 3, then no; (2) we only have to go through prime factors; because a composite ...

  4. cycloneii LAB-wide signals

    8 available LAB-width signals - 2 clocks - 2 clock enables - 2 asynchronous clears // - 1 asynchrono ...

  5. thinkPHP 字段映射功能

    thinkPHP的字段映射功能可以让你在表单中隐藏真正的数据表字段,而不用担心放弃自动创建表单对象的功能,假设我们的User表里面有username和email字段,我们需要映射成另外的字段,定义方式 ...

  6. JDK源码阅读--LinkedList

    public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, D ...

  7. 第一周——clone项目到本地

    公司使用的是git进行version control,代码托管在gitlab. 按照公司规范注册了gitlab账号, 漫长的等待clone到本地~ 然而,还是有问题,jar包下载不完全(公司网速dow ...

  8. HDU--2126 Buy the souvenirs(二维01背包)

    题目http://acm.hdu.edu.cn/showproblem.php?pid=2126 分析:有两个要求,一是计算最多可以选多少中纪念品:而是计算选最多纪念品的方案有多少种, 即统计最优方案 ...

  9. ASP.NET MVC生命周期与管道模型

      先来熟悉下asp.net请求管道 1.当客户端发送http://localhost:80/home/index请求时 2.首先到达服务端的内核模块HTTP.SYS(它监听80端口),通过访问注册表 ...

  10. 【论文翻译】NIN层论文中英对照翻译--(Network In Network)

    [论文翻译]NIN层论文中英对照翻译--(Network In Network) [开始时间]2018.09.27 [完成时间]2018.10.03 [论文翻译]NIN层论文中英对照翻译--(Netw ...