[英]C++ Polymorphism error: no matching function for call to
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: 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: 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: 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: Piece.h:
#ifndef PIECE_H
#define PIECE_H
using namespace std;
class Piece {
public:
Piece() {
}
};
#endif /* PIECE_H */
Space.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: 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: 基于对代码的检查,似乎setPieceObj()应该采用指针参数:
void Space::setPieceObj(Piece *p) {
pieceObj = p;
}
That's one problem. 这是一个问题。 Now, you need to pass a pointer to a Piece
object. 现在,您需要将指针传递给Piece
对象。
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(). 这是一个最令人烦恼的解析 ,它声明了一个返回Rook
的函数,并将指针传递给setPieceObj()。
There's not enough information to determine your clear intent. 没有足够的信息来确定您的明确意图。 Perhaps: 也许:
Rook bRook;
board[0][0].setPieceObj(&bRookL);
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
1. Rook bRookL();
1. Rook bRookL();
doesn't do what you expect, it's a declaration of function , which takes no parameters and returns Rook
, ie Rook (*)()
(just as compiler told you). 没有做你期望的,它是一个函数声明,它不带参数并返回Rook
,即Rook (*)()
(就像编译器告诉你的那样)。
2. &bRookL
will return the address of bRookL
, so it will be a pointer. 2. &bRookL
将返回的地址bRookL
,所以这将是一个指针。 But Space::setPieceObj(Piece &p)
expects its paramter to be Piece&
. 但是Space::setPieceObj(Piece &p)
期望它的参数是Piece&
。 It seems you should change the parameter type to Piece*
to make them consistent. 您似乎应该将参数类型更改为Piece*
以使它们保持一致。
Then 然后
Rook bRookL;
board[0][0].setPieceObj(&bRookL);
Pointer ( &bRookL
) can't be cast to reference ( Piece&
). 指针( &bRookL
)无法转换为引用( Piece&
)。
Most likely solution is in declaration of setPiece
that should take pointer: 最有可能的解决方案是声明setPiece
应该采用指针:
void setPieceObj(Piece*);
Which should actually make definition to be compilable too: 哪个应该实际上使定义也可以编译:
void Space::setPieceObj(Piece* p) {
pieceObj = p;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.