Problems with Template method
Aug 13, 2012 at 7:10pm UTC
Hey guys, I am new to classes in c++ and am getting this error whenever I compile my code, and it's driving me mad.
Error:
Main.cpp:(.text+0xe5): undefined reference to `Sniper::Sniper(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make: *** [task1] Error 1
Here is my code:
in main I have the following:
1 2 3
Survivor* sniper = new Sniper(names[0]);
sniper->attack(charger);
delete sniper;
in my Sniper.h for sniper i have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Sniper : public Survivor
{
public :
Sniper(string);
~Sniper();
/*virtual*/ bool hitZombie(Zombie*);
/*virtual*/ void celebrate();
/*virtual*/ bool getHit(Zombie*) ;
/*virtual*/ void die() ;
};
in my Sniper.cpp i have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
bool Sniper::hitZombie(Zombie* z)
{
int currentHP = 999;
currentHP = z->takeDamage(damage);
cout << "Sniper " << name << "fires a " << primaryWeapon << " at the zombie." << endl;
if (currentHP <= 0)
{
return true ;
}
else
return false ;
}
void Sniper::celebrate()
{
cout << name<< " exclaims \"Headshot!\" " <<endl;
}
bool Sniper::getHit(Zombie* z)
{
int zombieDamage = 0;
zombieDamage = z->getDamage();
hp = hp - zombieDamage;
cout << name << " swears in 13 different languages as he takes " << zombieDamage<< " damage." << endl;
if ( hp <= 0)
{
return true ;
}
else
return false ;
}
void Sniper::die()
{
cout << name << " lead a good life. He will be missed." <<endl;
}
in my Survivor.h i have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
class Survivor
{
protected :
string name;
int hp;
string primaryWeapon;
string secondaryWeapon;
int damage;
public :
// constructs a survivor with the specified name
Survivor(string);
~Survivor();
void attack(Zombie*);
virtual bool hitZombie(Zombie*) = 0;
virtual void celebrate() = 0;
virtual bool getHit(Zombie*) = 0;
virtual void die() = 0;
};
in my survivor.cpp i have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
Survivor::Survivor(string name)
: name(name), hp(1), primaryWeapon("Right Hand" ), secondaryWeapon("Left Hand" ), damage(1)
{
cout << "Recruiting a Survivor!" << endl;
}
Survivor::~Survivor()
{
}
void Survivor::attack(Zombie *z)
{
cout << name << " attacks a Zombie!" << endl;
while (hp > 0)
{
if (hitZombie(z))
{
// The zombie died, celebrate and move on with life.
celebrate();
return ;
}
else
{
if (getHit(z))
{
// Got killed by the zombie. Survivor now dies.
die();
}
}
}
}
Help in this matter will be greatly appreciated!!
Thanks
Last edited on Aug 13, 2012 at 8:40pm UTC
Aug 13, 2012 at 7:15pm UTC
Did you ever get the crazy idea to actually implement the constructor somewhere?
Aug 13, 2012 at 7:22pm UTC
I did but whenever i tried it threw errors back in my face. I am unsure how to implement it correctly if you could please help me with that.
Thanks
Topic archived. No new replies allowed.