【发布时间】:2014-02-04 10:52:50
【问题描述】:
我正在兜圈子里寻找错误消息,试图弄清楚如何才能得到我需要的工作。
我正在制作一个国际象棋游戏,其中每个棋子都是一个类,并且有一个名为 Piece 的接口类。
Piece.h
class Piece {
public:
virtual ~Piece() {};
/*
* Verifies the move on a per-piece basis. If the move is valid, make the changes on the board and return true, false otherwise.
*
* @param b Reference to the board
* @param toLocation Where to move the piece
* @return bool If the move is valid
*/
virtual bool move(Board &b, std::pair<int, int> toLocation) { return false; }
protected:
Piece(GameData::Color _c) { c = _c; }
GameData::Color c;
};
Pawn.h
class Pawn : Piece {
public:
Pawn(GameData::Color _c);
virtual ~Pawn();
bool move(Board &b, std::pair<int, int> toLocation);
};
但我无法让此设置正常工作。
我收到一个错误:
Pawn::Pawn(GameData::Color _c) : c(_c) {
no matching function for call to Piece::Piece()
我将 Piece 的可见性更改为:
class Pawn : public Piece
但是当我没有一个空的构造函数时,我会收到更多错误。
我正在设置它以尝试制作一个 2D 棋子数组来代表棋盘:
board = new Piece**[SIZE];
for(int i = 0; i < SIZE; ++i)
board[i] = new Piece*[SIZE];
/* Setup initial positions */
board[0][0] = new Rook(GameData::BLACK);
这就是为什么我不能让 move 方法纯粹是虚拟的。因为 new Piece* 调用抱怨它需要实现。
【问题讨论】:
-
Pawn::Pawn(GameData::Color _c) : Piece(_c) {}. -
很简单嗯..谢谢哈哈..
-
无数次重复中的一个:g++ no matching function call error
-
你应该将方法设为虚拟并找出为什么 new Piece*[SIZE];抱怨,因为它不应该。使特定方法成为纯虚拟方法是正确的做法。
标签: c++ oop inheritance interface