我正在使用带有-std = c ++ 11标志的gcc。在我的课程定义中,我有以下内容:
private:
const int January = 1,
February = 2,
March = 3,
...
在我的实现中,我有一个switch语句。
switch (currentMonth)
{
case January:
returnString = "January";
break;
case February:
returnString = "February";
break;
case March:
returnString = "March";
break;
...
这似乎应该有效,因为月份不变;但是,gcc给了我
calendar.cpp:116:12: error: ‘this’ is not a constant expression
关于switch语句的每一个案例。为什么这个错了?
答案 0 :(得分:13)
非静态类成员不是常量表达式。试试这个:
static constexpr int January = 1;
答案 1 :(得分:3)
试试这个:
enum {
January = 1,
February = 2
...
};