Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many initializers for Array error

Tags:

c++

arrays

object

I have implemented following structures:

struct Point {
    int x,y;
};

struct Array {
    Point elem[3];
};

Could you explain why I'm getting an error:

error: too many initializers for 'Array'

when I use following construction?:

Array points2 {{1,2},{3,4},{5,6}};
like image 369
Leopoldo Avatar asked Dec 02 '22 18:12

Leopoldo


2 Answers

You need more braces, since you're initialising objects within an array within a class:

Array points2 { { {1,2},{3,4},{5,6}}};
              ^ ^ ^
              | | |
              | | array element
              | array
              class
like image 182
Mike Seymour Avatar answered Dec 15 '22 16:12

Mike Seymour


You actually need one more set of braces like so:

Array points2 {{{1,2},{3,4},{5,6}}};

Working example

See this post for further explanation of when these extra braces are required. It is related to whether the container is an aggregate or not.

like image 31
Cory Kramer Avatar answered Dec 15 '22 15:12

Cory Kramer