Given the following example:
// Fighter.h
class Fighter {
public:
void attack(Fighter * other);
protected:
Fighter(const std::string nick) : nickname(nick) { };
virtual void atk(Fighter * other) = 0;
const std::string nickname;
};
// Fighter.cpp
#include "Fighter.h"
void Fighter::attack(Fighter * other) {
std::cout << nickname << " attacks " other->getNickname() << std::endl;
atk(other);
}
// Warrior.cpp : Fighter
void Warrior::atk(Fighter * other) {
int dmg = rand() % off;
int chance = rand() % 6;
if (chance == 3) {
dmg += dmg;
std::cout << nickname << ": Double Damage!";
}
other->hit(dmg);
if (!other->isDead()) {
other->counter(this);
}
}
// main.cpp
#include "Fighter.h"
#include "Warrior.h"
Fighter * a = new Warrior(string("A"));
Fighter * b = new Warrior(string("B"));
a->attack(b); // << LINKER ERROR
I only get this far:
undefined reference to `Fighter::attack(Fighter*)'
In other words, I want a base method which handles output for all derived classes so I don't have to duplicate the code in each subclass?