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&)
This is illegal:
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,
newis unnecessary and undesirable if the array size is fixed. Simply use this.Finally, the entries in the array are
Pieceobjects, not pointers. Initializing them toblankPiece(whatever that is) should be unnecessary because the default (no-argument) constructorPiece::Piece()should initialize the object to the blank state. To reinitialize, useIf the array size is variable, best practice is to use
std::vectorinstead ofnew[]. Here is the most common approach to two-dimensionalvector:That is a little ugly, and you might look into alternatives like
boost::multi_array.