I know this has been asked before (or some similar variations) however I cannot get it to work.
I am trying to create a board game that is composed of a board filled with squares. I am trying to model my board as a 2d array of Square objects. The board can be created with any width or height, and those parameters are passed in through the constructor. Here is the code I am working with:
Board.h
#ifndef BOARD_H
#define BOARD_H
#include "Square.h"
class Board{
public:
Board(int w, int h);
private:
Square **squares;
const int width;
const int height;
};
#endif
Board.cpp
#include "Board.h"
Board::Board(int w, int h):width(w), height(h) {
squares = new Square*[width];
for (int i = 0; i < width; i++) {
squares[i] = new Square[height];
}
}
However, when I try to compile this I get an error which seems to indicate the squares[i] = new Square[height]
is trying to call the default constructor for the Square object (which I do not want to exist nor call in this case).
Board.cpp: In constructor ‘Board::Board(int, int)’:
Board.cpp:7:33: error: no matching function for call to ‘Square::Square()’
squares[i] = new Square[height];
Any ideas? Is this possible within C++?