简体   繁体   中英

too many initializer values c++

Could someone explain why i'm getting the following compiler error:

error: too many initializers for 'std::array<std::array<State, 2>, 8>

#include <iostream>
#include <array>

using namespace std;

enum State
{
  NONE,
  WHITE,
  BLACK
};


int main()
{

  array<array<State, 2>, 8> initial = {
    { State::NONE, State::NONE },
    { State::WHITE, State::WHITE },
    { State::NONE, State::NONE },
    { State::NONE, State::NONE },
    { State::NONE, State::NONE },
    { State::NONE, State::NONE },
    { State::BLACK, State::BLACK },
    { State::NONE, State::NONE }
  };

  return 0;
}


You can either use a normal array like so :

State ini[8][2] = {
   { State::NONE, State::NONE },
  { State::WHITE, State::WHITE },
  { State::NONE, State::NONE },
  { State::NONE, State::NONE },
  { State::NONE, State::NONE },
  { State::NONE, State::NONE },
  { State::BLACK, State::BLACK },
  { State::NONE, State::NONE }
};

or to fix your code add a pair of brackets :

 array<array<State, 2>, 8> initial = { {
      {State::NONE, State::NONE },
      { State::WHITE, State::WHITE },
      { State::NONE, State::NONE },
      { State::NONE, State::NONE },
      { State::NONE, State::NONE },
      { State::NONE, State::NONE },
      { State::BLACK, State::BLACK },
      { State::NONE, State::NONE }
    } };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM