Neraxa
Neraxa

Reputation: 25

C++ Error: Too many initializers for multidimentional array

I have just begun practicing with multidimentional arrays, and thought I could create a map through it using characters. However i get the 'too many initializers' error and cant seem to figure out why.

char gameMap[5][5] = {

    {'.'},{'.'},{'.'},{'.'},{'.'},
    {'.'},{'.'},{'.'},{'.'},{'.'},
    {'.'},{'.'},{'.'},{'.'},{'.'},
    {'.'},{'.'},{'.'},{'.'},{'.'},
    {'.'},{'.'},{'.'},{'.'},{'.'}
    };

Upvotes: 2

Views: 173

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50775

You forgot some braces, you want this:

  char gameMap[5][5] = {
    { {'.'},{'.'},{'.'},{'.'},{'.'} },
    { {'.'},{'.'},{'.'},{'.'},{'.'} },
    { {'.'},{'.'},{'.'},{'.'},{'.'} },
    { {'.'},{'.'},{'.'},{'.'},{'.'} },
    { {'.'},{'.'},{'.'},{'.'},{'.'} },
  };

Your code was for initializing a one dimensional array of 25 elements.

But actually you should write this like this:

  char gameMap[5][5] = {    
    { '.','.','.','.','.'},
    { '.','.','.','.','.'},
    { '.','.','.','.','.'},
    { '.','.','.','.','.'},
    { '.','.','.','.','.'},
  };

But the best solutionn here is initializing programmatically:

for (int x < 0; x < 5; x++)
  for (int y < 0; y < 5; y++)
    gameMap[x][y] = '.';

Upvotes: 1

Related Questions