0

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

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 方法中执行此操作不会出现问题。所以我不知道为什么它给了我一个错误。非常感谢您提前提供的帮助。

4

1 回答 1

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};
于 2013-05-07T03:22:21.690 回答