2014-01-08 84 views
1

所以我試圖實現一個國際象棋遊戲。我已經得到了最常見的連接錯誤:沒有默認構造函數的C++繼承

error LNK2019: unresolved external symbol "public: __thiscall Bishop::~Bishop(void)" ([email protected]@[email protected]) referenced in function _main

因此,這裏有兩個相關的類:

Bishop.h(有沒有 「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(沒有 「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 

在「通配als.h「這些定義很少,但與我的功能無關。 所以我什麼我做了主很乾脆:

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

但它給了LNK2019錯誤。

現在我知道該解決方案應該是向兩個類添加默認構造函數(這是因爲某種原因不起作用),但我不希望它們使用默認值進行初始化。因此,我不希望任何這些類的默認構造函數。

有沒有什麼辦法,我不創建默認的構造函數,並擺脫它呢?

+1

編譯器抱怨你沒有定義'〜主教(無效) '但 – billz

+0

鏈接器抱怨缺少析構函數實現。與構造函數無關! –

+0

該死的......是的,這是它的感謝。 –

回答

5

您錯過了~ChessPiece(void);~Bishop(void);的定義。這正是編譯器所抱怨的。

另請注意,當您聲明ChessPiece(bool)時,默認的ChessPiece()構造函數不再可用:因此,您將無法默認構造一個ChessPiece

但如果你是在C++ 11並要手動刪除默認的構造函數的一些原因,你可以使用:

ChessPiece() = delete; 
5

你的問題是關於析構函數:

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

你聲明的析構函數但它沒有提供一個實現。