1

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 valueserror. Why is this happening?

EDIT:
The red squiggly line underlines the second element of the array.

5
  • It looks like you're trying to assign to an array. Please post an MCVE. Commented Oct 25, 2014 at 14:45
  • What is MCVE? I don't know that. Yes, I am trying to assign values in the constructor. Commented Oct 25, 2014 at 14:46
  • Your array size is 64 and you are probably initializing it more elements. Commented Oct 25, 2014 at 14:48
  • 1
    Minimal, Complete, and Verifiable example. Commented Oct 25, 2014 at 14:49
  • As for your problem, what is the complete declaration? Commented Oct 25, 2014 at 14:49

2 Answers 2

2
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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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
{
}

3 Comments

No, that would be 65th element.
he's using Visual C++., it does not support explicit initializer for arrays
I need to initialise around 5-6 arrays, and 5 constant variables. It will look ugly if I put them in the initialiser list. Is there any other method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.