我试图将一个子对象传递给一个接受其父对象的代码的函数:
Rook bRookL(); board[0][0].setPieceObj(bRookL);我得到以下错误:
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&'这是我的代码:
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; }请帮忙。 谢谢。
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.
最满意答案
看来,基于你的代码的检查,setPieceObj()应该可能带有一个指针参数:
void Space::setPieceObj(Piece *p) { pieceObj = p; }这是一个问题。 现在,你需要传递一个指向Piece对象的指针。
Rook bRookL(); board[0][0].setPieceObj(&bRookL);这是一个令人烦恼的解析 ,它声明了一个返回Rook的函数,并将一个指针传递给setPieceObj()。
没有足够的信息来确定您的明确意图。 也许:
Rook bRook; board[0][0].setPieceObj(&bRookL);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);更多推荐
发布评论