Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
790 views
in Technique[技术] by (71.8m points)

arrays - 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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...