too many initializers for #39;int [0]#39; c++(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.
这篇关于'int [0]' C++ 的初始化程序太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:'int [0]' C++ 的初始化程序太多


- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- Stroustrup 的 Simple_window.h 2022-01-01
- 静态初始化顺序失败 2022-01-01