Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

too many initializers for 'int [0]' c++

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];

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

like image 711
cppython Avatar asked Jan 16 '14 02:01

cppython


1 Answers

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.

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.

like image 103
jtomschroeder Avatar answered Oct 04 '22 22:10

jtomschroeder