太多的初始化为'INT [0]'C ++ [英] too many initializers for 'int [0]' c++

查看:114
本文介绍了太多的初始化为'INT [0]'C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:

int k[] ={1,2,3,4,5};

第二:

struct slk
{
    int k[] ={1,2,3,4,5};
};

对于那些两个语句,为什么第一个通过编译但第二个给我
错误:太多初始化为'INT [0]'。如果我设置k [5]编辑会通过;

for those two statements, why does the first one pass the compilation but the second one give me error:too many initializers for 'int [0]'. the compilation would passed if I set k[5];

这是什么错误消息意味着什么?注意:code对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 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天全站免登陆