문제

So I was trying to implement a chess game. I've got the most common link error:

error LNK2019: unresolved external symbol "public: __thiscall Bishop::~Bishop(void)" (??1Bishop@@QAE@XZ) referenced in function _main

So here are the two related classes:

Bishop.h (There is no "Bishop.cpp")

#pragma once
#ifndef BISHOP_H
#define BISHOP_H

#include "ChessPiece.h"

class Bishop : public ChessPiece
{
public:
    Bishop(bool isWhite) : ChessPiece(isWhite) {
    }
    ~Bishop(void);

    // pure virtual functions
    virtual CellLocation *listAvailableMoves(void) {
        return 0;
    }

    virtual char getPieceType() {
        return PIECE_TYPE_BISHOP;
    }
};

#endif

ChessPiece.h (there is no "ChessPiece.cpp")

#ifndef CHESSPIECE_H
#define CHESSPIECE_H

#include "Globals.h"

// Abstract class for inheritence
class ChessPiece {
public:
    // Constructor
    ChessPiece(bool isWhite) : m_isWhite(isWhite) {
    }
    ~ChessPiece(void);

    // pure virtual functions
    virtual CellLocation *listAvailableMoves(void) = 0;
    virtual char getPieceType() = 0;

    // ACCESSORS, MUTATORS
    // isWhite member
    bool isWhite(void) const{
        return m_isWhite;
    }

    void setIsWhite(bool isWhite) {
        m_isWhite = isWhite;
    }

    protected:
        bool m_isWhite;
    };
    #endif

In the "Globals.h" there are few definitions of these but it's unrelated to my function. So I what I've done in main was simply:

Bishop somePiece(true);
cout << sizeof(somePiece) << endl;

But it gave out the LNK2019 error.

Now I know that the solution should be to adding default constructors to both classes (which didn't work for some reason) but I don't want them to be initialized with default values. Hence I don't want default constructors for any of these classes.

Is there any way that I do not create default constructors and get away with it?

도움이 되었습니까?

해결책

You are missing the definitions of ~ChessPiece(void); and ~Bishop(void);. That's what the compiler is complaining about.

Also notice that when you declare ChessPiece(bool), the default ChessPiece() constructor is not available anymore: hence you won't be able to default construct a ChessPiece.

But if you are on C++11 and for some reasons you want to delete the default constructor manually, you can use:

ChessPiece() = delete;

다른 팁

Your problem is about the destructor:

unresolved external symbol "public: __thiscall Bishop::~Bishop(void)"
                                                       ^
//                                                Notice the ~

You declared a destructor but didn't provide an implementation for it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top