I'm trying to pass a child object to a function that accepts its parent object with the code:
Rook bRookL();
board[0][0].setPieceObj(bRookL);
and I get the following errors:
ChessBoard.cpp:16:35: error: no matching function for call to 'Space::setPieceObj(Rook (&)())'
board[0][0].setPieceObj(bRookL);
^
In file included from ChessBoard.h:13:0,
from ChessBoard.cpp:5:
Space.h:62:10: note: candidate: void Space::setPieceObj(Piece&)
void setPieceObj(Piece &);
^
Space.h:62:10: note: no known conversion for argument 1 from 'Rook()' to 'Piece&'
Here is my code:
ChessBoard.h:
#ifndef CHESSBOARD_H
#define CHESSBOARD_H
#include "Space.h"
using namespace std;
class ChessBoard {
private:
Space board[8][8]; // board[0][0] is the upper-left corner of the board and
// board[7][7] is the lower-right corner of the board
public:
ChessBoard();
};
#endif /* CHESSBOARD_H */
ChessBoard.cpp:
#include "ChessBoard.h"
#include "Space.h"
#include "Rook.h"
using namespace std;
ChessBoard::ChessBoard() {
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
}
Rook.h:
#ifndef ROOK_H
#define ROOK_H
#include "Piece.h"
using namespace std;
class Rook : public Piece {
public:
Rook() : Piece() {
}
};
#endif /* ROOK_H */
Piece.h:
#ifndef PIECE_H
#define PIECE_H
using namespace std;
class Piece {
public:
Piece() {
}
};
#endif /* PIECE_H */
Space.h:
#ifndef SPACE_H
#define SPACE_H
#include "Piece.h"
#include "Rook.h"
using namespace std;
class Space {
private:
Piece *pieceObj;
public:
void setPieceObj(Piece &);
};
#endif /* SPACE_H */
Space.cpp:
#include "Space.h"
using namespace std;
Space::Space() {
}
void Space::setPieceObj(Piece &p) {
pieceObj = p;
}
Please help. Thanks.
It appears, based on the examination of your code, that setPieceObj() should probably take a pointer argument:
void Space::setPieceObj(Piece *p) {
pieceObj = p;
}
That's one problem. Now, you need to pass a pointer to a Piece object.
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
That's a most vexing parse, that declares a function that returns a Rook, and passes a pointer to it, to setPieceObj().
There's not enough information to determine your clear intent. Perhaps:
Rook bRook;
board[0][0].setPieceObj(&bRookL);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With