2014-05-10 78 views
0

我正在做一個家庭作業問題,我們必須使用繼承。 (我還沒有很好的繼承)。我們要製作一個父類「card_games」,它有兩個子類「gofish」和「poker」。我們獲得了我們主要關注的模板,其餘設計由我們決定。這裏是我的頭文件(稱爲「classes.h」)錯誤LNK2001繼承問題

#include <vector> 

using namespace std; 

struct cards{ 
    int rank; 
    char suit; 
}; 

class players{ 
    public: 
    int points; 
    int active; 
    vector<cards> cardhand; 
    void printhand(players *gameplayers, int person); 
}; 

class card_games{ 
    protected: 
    players *gameplayers; 
    void player_make(); 
    public: 
    virtual void play(); 
}; 


class poker :public card_games{ 
    public: 
    void play(); 
}; 


class gofish :public card_games{ 
    public: 
void play(); 
    }; 





void player0_play(players *gameplayers, cards *cardlist, int people); 
void createdeck(cards *cardlist); 
void shuffle(cards *cardlist); 
void deal(cards *cardlist, int people, players *gameplayers); 
int getplayers(); 

我已經確定了錯誤的東西與我的虛擬呼叫。錯誤具體爲:

cardgames_play.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall card_games::play(void)" ([email protected][email protected]@UAEXXZ) 

,我相信這會導致我的下一個錯誤:

card_games.exe : fatal error LNK1120: 1 unresolved externals 

反正東西是錯誤與我的虛擬無效的功能。 這裏是我的主要功能:

#include <iostream> 
#include "classes.h" 



int main(){ 
    card_games *game; 
    int opt; 
    game = NULL; 
    cout << "Poker 1, Go Fish 2" << endl; 
    cin >> opt; 
    if (opt == 1) 
    game = new poker; 
    else if (opt == 2) 
    game = new gofish; 
    game->play(); 

    return 0; 
} 

我們都應該大致使用此模板。如果我理解正確,我創建一個名爲game的Card_game類的實例,然後將遊戲分配給gofish或poker的實例。然後我將「遊戲」解除引用到「play();」功能。我的代碼的其餘部分,我有一個

void gofish::play(){ blah blah }

void poker::play(){ 
blah blah 
} 

其中有我的代碼,工程的其餘部分。

有關此錯誤的任何幫助,非常感謝。謝謝!

P.S.我在Windows 8上使用visual studio 2013.

+0

要麼讓card_games ::玩(在聲明= 0)純虛或給它一個體({})在聲明 – cup

+0

始終是務必閱讀時出現的說明選擇標籤! – Charles

回答

1

方法void play(); in card_games沒有body,並且它不是純虛擬的。只是做了改變:

class card_games{ 
protected: 
    players *gameplayers; 
    void player_make(); 
public: 
    virtual void play()=0; //make it pure virtual 
}; 
+0

非常感謝! – Gigaxalus

+0

@ user2951303, 如果這個或任何答案已經解決了您的問題,請點擊複選標記,考慮 [接受它](http://meta.stackexchange.com/q/5234/179419)。 這表示您已經找到了解決方案,並向回答者和您自己提供了一些名譽。沒有義務這樣做。 – Rakib