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;