C多态性错误:没有匹配函数用于调用

我正在尝试将子对象传递给一个函数,该函数接受其父对象的代码:

Rook *L();
board[0][0].setPieceObj(*L);

我收到以下错误:

    ChessBoard.cpp:16:35: error: no matching function for call to 'Space::setPieceObj(Rook (&)())'
     board[0][0].setPieceObj(*L);
                                   ^
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 *L();
    board[0][0].setPieceObj(&*L);

}

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;

}

请帮忙.谢谢.

解决方法:

基于对代码的检查,似乎setPieceObj()应该采用指针参数:

void Space::setPieceObj(Piece *p) {

    pieceObj = p;

}

这是一个问题.现在,您需要将指针传递给Piece对象.

Rook *L();
board[0][0].setPieceObj(&*L);

这是一个most vexing parse,它声明一个返回Rook的函数,并将指针传递给setPieceObj().

没有足够的信息来确定您的明确意图.也许:

Rook *;
board[0][0].setPieceObj(&*L);
上一篇:php – Laravel多态关系:将模型传递给控制器


下一篇:Python中的多态性