Click here to Skip to main content
14,668,844 members
Rate this:
Please Sign up or sign in to vote.
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 1: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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100