I am building a simple chess application and I've been finding some issues. Here is my problem:
I have a class Piece:
class Piece {
protected:
std::string name; // this is to store the piece's name (bishop, rook, etc.)
public:
Piece();
int positionX;
int positionY;
std::string getName();
};
And I also have a class Board, which has a 8x8 matrix of Pieces:
class Board {
public:
// the construct fills the matrix with instances from Piece just so all matrix slots will have a piece
Board();
void showBoard();
std::vector<std::vector<Piece>> piece;
void place(Piece piece, char positionY, int positionX);
};
At the moment, I have a board object with 64 generic Pieces.
Now, I created a class Rook, which inherits from Piece:
class Rook: public Piece {
private:
std::string color;
public:
Rook(std::string color);
std::string getColor();
bool isRook = true; // dummy attribute just to show you my problem
};
And now I try to place my rook inside my pieces matrix:
Board board;
Rook rook = Rook("white");
rook.positionX = 3;
rook.positionY = 0;
board.piece.at(rook.positionX).at(rook.positionY) = rook;
std::cout << board.piece.at(3).at(0).positionX << std::endl; // as expected, this outputs 3
Finally, here comes my actual issue:
When I try to access something that only rook has, and not the other pieces, for example the bool isRook, it throws an error that Piece class has no attribute isRook:
std::cout << board.piece.at(3).at(0).isRook << std::endl; // this throws an error "class Piece has no attribute isRook"
I assume this is because I have a matrix of Pieces, so I can't access things that are specific to Rooks. How can I solve this issue?