简体   繁体   中英

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:

#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.

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);
Rook bRookL();
board[0][0].setPieceObj(&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).

2. &bRookL will return the address of bRookL , so it will be a pointer. But Space::setPieceObj(Piece &p) expects its paramter to be Piece& . It seems you should change the parameter type to Piece* to make them consistent.

Then

Rook bRookL;
board[0][0].setPieceObj(&bRookL);

Pointer ( &bRookL ) can't be cast to reference ( Piece& ).

Most likely solution is in declaration of setPiece that should take pointer:

void setPieceObj(Piece*);

Which should actually make definition to be compilable too:

void Space::setPieceObj(Piece* p) {
    pieceObj = p;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM