打开大括号会导致语法错误

时间:2013-05-07 03:16:01

标签: c++ arrays syntax

我无法将元素初始化为我班级中的整数数组。为了简洁起见,代码被裁剪:

class Update
{
private:
static const int MONTHS_IN_YEAR = 12; // months in a year
static const int dayCounts [MONTHS_IN_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
};

出于某种原因,在我的课程中执行此操作会导致语法错误,但在main方法中执行此操作会导致没有问题。所以我不知道为什么它会给我一个错误。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

您需要constexpr才能发挥作用:

static constexpr int dayCounts [MONTHS_IN_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

但是,您的编译器(visual C ++)尚不支持此功能。因此,您必须在课外进行初始化:

const int Update::dayCounts [MONTHS_IN_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};