1

I'm trying to make a simple game in c++, I'm almost complete but I keep running into this error. I'm sure these are syntax errors, I'm just not sure on how to fix them.

Board::Board()
{
    side = 6;
    Piece[][] spaces = new Piece[6][6];
    for (int row = 00; row < side; ++row)
    {
        for (int column = 0; column < side; ++column)
        {
            spaces[row][column] = new blankPiece;
        }
    }
}

Here is what eclipse is saying:

..\Board.cpp: In constructor 'Board::Board()':
..\Board.cpp:13:7: error: expected unqualified-id before '[' token
..\Board.cpp:18:30: error: no match for 'operator=' in '((Board*)this)->Board::spaces[row][column] = (operator new(8u), (<statement>, ((blankPiece*)<anonymous>)))'
..\/Piece.h:14:1: note: candidate is: Piece& Piece::operator=(const Piece&)
  • 1
    possible duplicate of [\[C++\] How do I declare a 2d array using new?](http://stackoverflow.com/questions/936687/c-how-do-i-declare-a-2d-array-using-new) – GWW Apr 07 '11 at 01:59

5 Answers5

4

This is illegal:

Piece[][]

C++ does not store a 2-dimensional array as an array of pointers, but rather a flat series of subarrays. Therefore all but the last bound must be specified.

Also, new is unnecessary and undesirable if the array size is fixed. Simply use this.

Piece spaces[6][6]; // that's all!

Finally, the entries in the array are Piece objects, not pointers. Initializing them to blankPiece (whatever that is) should be unnecessary because the default (no-argument) constructor Piece::Piece() should initialize the object to the blank state. To reinitialize, use

myPiece = Piece();

If the array size is variable, best practice is to use std::vector instead of new[]. Here is the most common approach to two-dimensional vector:

typedef vector< vector< Piece > > Board;
Board spaces( 6, vector< Piece >( 6 ) );

That is a little ugly, and you might look into alternatives like boost::multi_array.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • thanks a lot! that worked, I know I'm not supposed to use arrays, but I'm still not familiar with vectors as I am somewhat new to c++, I will definitely try vector alternative in a copy of this game to get a good grasp. I will also check out boost multi_array for future. Everything worked out pretty good, just gotta make a few minor fixes and test the game. Once again, thanks! – Ronald A. Richardson Apr 07 '11 at 02:25
2

It's this line: Piece[][] spaces = new Piece[6][6];

That's not how C++ constructs type names for arrays. Try:

Piece (*spaces)[6] = new Piece[6][6];
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

I don't think you can do Piece[][], try:

Piece** spaces = new Piece*[6];
for(int i = 0; i < 6; i++) {
    spaces[i] = new Piece[6];
}

Or use boost::multi_array.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
1

Use just this

Piece spaces[6][6];

Mr. Zen
  • 704
  • 3
  • 7
  • 17
0

You cannot declare an array with more than 1 unknown dimension. Use pointers or std::vector instead.

Also, you allocate memory which you keep in a local variable spaces that disappears once you're out of the function, it should be a class member.

littleadv
  • 20,100
  • 2
  • 36
  • 50