I am trying to defined and initialize an array of struct.
#include <iostream>
#include <array>
int main() {
struct row{
double a0;
double a1;
};
//method 0: this way works
row c[2] ={{1.0,2.0},{3.0,4.0}};
//method 1: declare and initialization in same line
//std::array<row, 2> a = { {1.0, 2.0}, {3.0, 4.0} };//error: Excess elements in struct initializer
std::array<row, 2> a = {{ {1.0, 2.0}, {3.0, 4.0} }}; //double brace
//method 2, declare, then initialize in different line
std::array<row, 2> b;
//b = { {1.0, 2.0}, {3.0, 4.0} };//error: No viable overloaded '='
b = { { {1.0, 2.0}, {3.0, 4.0} } }; //double brace
return 0;
}
Now I find double brace works from this post.
Just wondering why do we need extra pair of brace to initialize array of struct?