'int [0]' C++ 的初始化程序太多
第一:
int k[] ={1,2,3,4,5};
第二:
struct slk
{
int k[] ={1,2,3,4,5};
};
对于这两个语句,为什么第一个通过编译但第二个给我
for those two statements, why does the first one pass the compilation but the second one give me
错误:'int [0]' 的初始化程序太多.如果我设置 k[5],编译将通过;
error:too many initializers for 'int [0]'. the compilation would passed if I set k[5];
这个错误信息是什么意思?注意:在 GNU GCC 版本 4.7.2 上测试的代码
What does this error message means? Note: code tested on GNU GCC version 4.7.2
推荐答案
在 C++11 中,允许使用类内成员初始化器,但基本上与在成员初始化列表中初始化相同.因此,必须明确说明数组的大小.
In C++11, in-class member initializers are allowed, but basically act the same as initializing in a member initialization list. Therefore, the size of the array must be explicitly stated.
Stroustrup 在他的网站这里有一个简短的解释.
Stroustrup has a short explanation on his website here.
错误消息意味着您为长度为 0 的数组提供了太多项,这正是 int []
在该上下文中的计算结果.
The error message means that you are providing too many items for an array of length 0, which is what int []
evaluates to in that context.
相关文章