Click here to Skip to main content
14,700,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone.
I tried to convert some intrinsic arrays into std::arrays in a C++ program with Visual Studio 2010.

No wonder this one could compile.
typedef struct {
    int a;
    int b;
} A;

A arr[2] = {
    { 0, 1 },
    { 2, 3 }
};

But this one couldn't compile. It caused an error "error C2078: too many initializers".
std::array<A, 2> arr = {
    { 0, 1 },
    { 2, 3 }
};

After trial and error for a while, I found that this one could compile.
(A couple of curly braces were added.)
std::array<A, 2> arr = {{
    { 0, 1 },
    { 2, 3 }
}};

What is the cause or the reason? Somebody please explain it.
Posted
Updated 24-Sep-10 2:38am
v2
Comments
Emilio Garavaglia 24-Sep-10 5:29am
   
Is C++0x support enabled?
Tsuda Kageyu 24-Sep-10 7:38am
   
Yes. I don't know how to disable it with VC++ 2010.
Ajay Vijayvargiya 27-Sep-10 14:53pm
   
Your question is well answered, this is about disabling C++0x, and other VC10 stuff.
Just change the compiler toolset (in project properties), from v100 to v90. You *must* have VS 2008 installed.

1 solution

I would use
A local_element;
local_element.a = 0;
local_element.b = 1;
arr.push(local_element);

local_element.a = 2;
local_element.b = 3;
arr.push(local_element);



or

const std::array<a,> arr = {    A( 0, 1 ),    A( 2, 3 )};


if you will write the constructor.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900