I'm writing a chess application. The Board
class contains an array of Square
, each of which can hold a Piece
. Type-specific classes (Pawn
, Rook
, etc.) inherit from Piece
.
In order to accomplish this, Square
has a member variable which points to a particular Piece
(which is occupying that Square
).
The trouble I'm having is that when I try to set up the Board
, I am unable to assign the unique_ptr
that I've created to the member variable of the Square
.
Here's the general stream of function calls:
void Board::setBoard()
{
// White Pawn Placement
std::unique_ptr<Piece> wp1 = std::make_unique<Pawn>(PAWN, WHITE);
Board::setPiece("a2", wp1);
}
↓
Pawn::Pawn(Type t, Color c) : Piece(t, c) {}
↓
void Board::setPiece(const std::string &coords, std::unique_ptr<Piece> piece)
{
squareMap[coords].setPiece(piece);
}
↓
void Square::setPiece(std::unique_ptr<Piece> piece)
{
Square::piece = std::move(piece);
}
I receive the following error when I attempt to compile at the line holding Board::setPiece("a2", wp1);
error: call to implicitly-deleted copy constructor of 'std::unique_ptr<Piece>'
which is, needless to say, a bit of a mouthful.
There is some good documentation online about inheritance, abstract classes, how to use the unique_ptr
, etc., but I've been unable to figure out how all of those things fit together.
So, the question:
How can I create an object, assign it to a unique_ptr, and then use that unique_ptr to set the member variable of another object?
Here are the header files for each class, in case that is illuminating. And please forgive the length of my question. I've made it as short as I can.
Board.hpp
class Board {
public:
Board();
void printBoard();
Piece getPiece(const std::string &coords);
void setPiece(const std::string &coords, std::unique_ptr<Piece> piece);
~Board();
...
};
Square.hpp
class Square
{
public:
void setPiece(std::unique_ptr<Piece> piece);
Piece* getPiece() const;
protected:
std::unique_ptr<Piece> piece;
...
};
Piece.hpp
class Piece
{
public:
Piece();
Piece(Type, Color);
virtual bool movePiece() = 0; // abstract class
protected:
Color color;
Type type;
bool moved;
...
};
Pawn.hpp
class Pawn : public Piece
{
public:
Pawn(Type t, Color c);
bool movePiece() override;
};