我试图让这个基于僵尸/人类代理的模拟运行,但是我遇到了这些具有父类“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) {...}