-1

我试图让这个基于僵尸/人类代理的模拟运行,但是我遇到了这些具有父类“Creature”的派生类(人类和僵尸)的问题。我在“Creature”中声明了 3 个虚函数,所有这三个虚函数都在“Human”和“Zombie”中重新声明和定义。但是由于某种原因,当我让我的程序调用“new”来为 Human 或 Zombie 类型的对象分配内存时,它抱怨虚函数是抽象的。这是代码:

标题:

class Creature
{          
public:
      virtual void Attack(Grid G) =0;
      virtual void AttackCreature(Grid G, int attackdirection) =0;
      virtual void Breed(Grid G) =0;
      virtual ~Creature() {}          
      void Die();
      void Move(Grid G);
      int DecideSquare(Grid G);
      void MoveTo(Grid G, int dir);           
};

class Human : public Creature 
{
public:          
      void Attack(Grid G);
      void AttackCreature(Grid G, int attackdirection);
      void Breed(Grid G); //will breed after x steps and next to human          
      int DecideAttack(Grid G);
};

class Zombie : public Creature
{
   public:
      void Attack(Grid G); 
      void AttackCreature(Grid G, int attackdirection);
      void Breed(Grid G) {} //does nothing  
      int DecideAttack(Grid G);
};

cp:

void Creature::Move(Grid G) {...}
int Creature::DecideSquare(Grid G) {...}
void Creature::MoveTo(Grid G, int dir) {...}
void Creature::Die() {...}
void Human::Breed(Grid G) {...}   
int Human::DecideAttack(Grid G) {...}
void Human::AttackCreature(Grid G, int attackdirection) {...}
int Zombie::DecideAttack(Grid G) {...}
void Zombie::AttackCreature(Grid G, int attackdirection) {...}
void Zombie::Attack(Grid G) {...}
4

2 回答 2

3

Creature

virtual void Breed() =0;

Human

void Breed(Grid G);

这些是不同的功能;后者不会覆盖前者。使纯虚函数Creature采用适当的参数。

对于Attack().

于 2012-11-24T23:05:13.650 回答
0

声明了一个纯虚函数:virtual void Creature::Attack(Grid G)但没有实现派生类Human,即没有void Human::Attack(Grid G)定义方法,这就是我收到vtable错误的原因。它找不到我承诺的功能的源代码。坏我!

于 2012-11-26T19:11:38.890 回答