C ++多态性错误:没有用于调用的匹配函数(C++ Polymorphism error: no matching function for call to)

编程入门 行业动态 更新时间:2024-10-22 07:29:17
C ++多态性错误:没有用于调用的匹配函数(C++ Polymorphism error: no matching function for call to)

我试图将一个子对象传递给一个接受其父对象的代码的函数:

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);

更多推荐

本文发布于:2023-08-07 02:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多态性   函数   错误   Polymorphism   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!