I am trying to get this Zombie/Human agent based simulation running, but I am having problems with these derived classes (Human and Zombie) who have parent class “Creature”. I have 3 virtual functions declared in “Creature” and all three of these are re-declared AND DEFINED in both “Human” and “Zombie”. But for some reason when I have my program call “new” to allocate memory for objects of type Human or Zombie, it complains about the virtual functions being abstract. Here’s the code:
header:
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);
};
cpp:
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) {...}
There was a pure virtual function declared:
virtual void Creature::Attack(Grid G)but there was no implementation for the derived classHuman, i.e. there was novoid Human::Attack(Grid G)method defined, which is why I was getting avtableerror. It couldn’t find the source code for the function that I promised. Bad me!