“int [0]"C++ 的初始值设定项太多 [英] too many initializers for 'int [0]' c++

查看:69
本文介绍了“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++ 的初始值设定项太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆