Reputation: 1603
I am trying to initialize an array in Visual C++.
In my header file, I am declaring the array like this.
int pawnSquareTable[64];
In the cpp file where I include the header file, I am initializing the array in the constructor of the class in this manner:
pawnSquareTable[64]={0,0,1,2.....64 values};
However, VC++ is giving me a Too many initializer values
error. Why is this happening?
EDIT:
The red squiggly line underlines the second element of the array.
Upvotes: 1
Views: 10028
Reputation: 7644
A::A()
// : pawnSquareTable{1,2,3,4} // this would compile in clang/gcc
{
// for MSVC, instead do this
int* p = pawnSquareTable;
for( int i : {1,2,3,4} ) // <- values here
*p++=i;
}
Upvotes: 2
Reputation: 27250
When you have the code pawnSquareTable[64]={0,0,1,2.....64 values};
in your constructor, you are actually trying to set the value for the single element pawnSquareTable[64]
(65th element of the array). The compiler expects to get an int and not an initializer-list, that's the reason for the error.
Instead of doing it, you should initialize the array in constructor's initialization list:
A::A() : pawnSquareTable{ 0, 1, 2 } //fill your values
{
}
Upvotes: 2