// foo.hpp file
class foo
{
public:
static const int nmConst;
int arr[nmConst]; // line 7
};
// foo.cpp file
const int foo::nmConst= 5;
编译器VC 2015返回错误:
1
为什么?nmConst 是静态常量,其值在 *.cpp 文件中定义。
可以使用< code>static const int成员作为数组大小,但是您必须在。hpp的文件是这样的:
class foo
{
public:
static const int nmConst = 10;
int arr[nmConst];
};
这会有用的。
附言:关于它背后的逻辑,我相信编译器一遇到类声明就想知道数组成员的大小。如果你在类中留下静态const int
未定义的成员,编译器会理解你正在尝试定义可变长度数组并报告错误(它不会等待看你是否真的在某个地方定义了nmconst
)。