对于“int[0]”c++的初始化器太多了。

时间:2022-09-01 19:20:58

First:

第一:

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

Second:

第二:

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

对于这两个表述,为什么第一个通过编译,而第二个给出了我

error:too many initializers for 'int [0]'. the compilation would passed if I set k[5];

错误:“int[0]”的初始化器太多。如果我设置k[5],编译就会通过;

What does this error message means? Note: code tested on GNU GCC version 4.7.2

这个错误消息意味着什么?注意:在GNU GCC版本4.7.2上测试代码。

4 个解决方案

#1


23  

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.

在c++ 11中,允许类内成员初始化器,但基本上与在成员初始化列表中初始化相同。因此,必须显式地声明数组的大小。

Stroustrup has a short explanation on his website here.

Stroustrup在他的网站上有一个简短的解释。

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.

错误消息意味着为长度为0的数组提供了太多的项,这是int[]在该上下文中计算的值。

#2


3  

In the first example, something (actual memory allocation) is actually happening. The computer easily counts the number of items given to it and uses this as the capacity. In the second use, as part of a struct, the array is simply part of the template of a struct. In the second case, a size must explicitly be given and known at compile-time. There is no automatic counting here. It's in a similar vein to function declarations versus definitions as well as variable declarations (tells type but memory is untouched) and their invocation/use (where the program acts).

在第一个示例中,一些事情(实际内存分配)实际上正在发生。计算机可以很容易地计算给它的项的数量,并以此作为容量。在第二次使用中,作为struct的一部分,数组只是结构模板的一部分。在第二种情况下,必须在编译时显式地给出和知道大小。这里没有自动计数。它与函数声明、定义以及变量声明(告知类型但不涉及内存)以及它们的调用/使用(程序在其中执行)类似。

#3


1  

Probably cause in a struct the compiler needs you to specify the size explicitly.

可能是因为在结构体中,编译器需要您显式地指定大小。

C initialize array within structure (pun intended)

C在结构中初始化数组(双关)

#4


1  

These are two completely different contexts:

这是两个完全不同的上下文:

The first is a variable declaration (with an initialiser clause).

第一个是变量声明(带有initialiser子句)。

The second is a type definition.

第二个是类型定义。

#1


23  

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.

在c++ 11中,允许类内成员初始化器,但基本上与在成员初始化列表中初始化相同。因此,必须显式地声明数组的大小。

Stroustrup has a short explanation on his website here.

Stroustrup在他的网站上有一个简短的解释。

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.

错误消息意味着为长度为0的数组提供了太多的项,这是int[]在该上下文中计算的值。

#2


3  

In the first example, something (actual memory allocation) is actually happening. The computer easily counts the number of items given to it and uses this as the capacity. In the second use, as part of a struct, the array is simply part of the template of a struct. In the second case, a size must explicitly be given and known at compile-time. There is no automatic counting here. It's in a similar vein to function declarations versus definitions as well as variable declarations (tells type but memory is untouched) and their invocation/use (where the program acts).

在第一个示例中,一些事情(实际内存分配)实际上正在发生。计算机可以很容易地计算给它的项的数量,并以此作为容量。在第二次使用中,作为struct的一部分,数组只是结构模板的一部分。在第二种情况下,必须在编译时显式地给出和知道大小。这里没有自动计数。它与函数声明、定义以及变量声明(告知类型但不涉及内存)以及它们的调用/使用(程序在其中执行)类似。

#3


1  

Probably cause in a struct the compiler needs you to specify the size explicitly.

可能是因为在结构体中,编译器需要您显式地指定大小。

C initialize array within structure (pun intended)

C在结构中初始化数组(双关)

#4


1  

These are two completely different contexts:

这是两个完全不同的上下文:

The first is a variable declaration (with an initialiser clause).

第一个是变量声明(带有initialiser子句)。

The second is a type definition.

第二个是类型定义。