我正在解决一个家庭作业问题,其中我们必须使用继承。(我还不太擅长继承)。我们将创建一个父类"card_games“,它有两个子类,分别称为"gofish”和"poker“。我们得到了一个模板,我们的main必须遵循这个模板,其余的设计都取决于我们。这是我的头文件:(名为"classes.h")
#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)" (?play@card_games@@UAEXXZ)我相信这会导致我的下一个错误:
card_games.exe : fatal error LNK1120: 1 unresolved externals不管怎样,我的虚拟void函数出了点问题。下面是我的主要函数:
#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;
}我们应该大致使用这个模板。如果我理解正确,我创建了一个名为Card_game的游戏类的实例,然后将游戏分配给gofish或poker的一个实例。然后我将“游戏”取消对"play();“函数的引用。剩下的代码,我有一个
void gofish::play(){ blah blah }
和一个
void poker::play(){
blah blah
}其中有我的其余代码可以正常工作。
任何关于这个错误的帮助我们都非常感谢。谢谢!
附注:我在windows 8上使用的是visual studio 2013。
发布于 2014-05-10 13:29:10
card_games中的方法void play();没有主体,并且它不是纯虚拟的。只需进行更改即可:
class card_games{
protected:
players *gameplayers;
void player_make();
public:
virtual void play()=0; //make it pure virtual
};https://stackoverflow.com/questions/23577390
复制相似问题