Hi guys!
I am getting the error:
SniperFactory.cpp: In member function ‘virtual Survivor* SniperFactory::createSurvivor(std::string)’:
SniperFactory.cpp:7:39: error: cannot allocate an object of abstract type ‘Survivor’
Survivor.h:9:7: note: because the following virtual functions are pure within ‘Survivor’:
Survivor.h:32:15: note: virtual bool Survivor::hitZombie(Zombie*)
Survivor.h:33:15: note: virtual void Survivor::celebrate()
Survivor.h:34:15: note: virtual bool Survivor::getHit(Zombie*)
Survivor.h:35:15: note: virtual void Survivor::die()
Here is my code:
My code is organised here as follows: (1) the relevant statements from main.cpp, (2) a base factory class (as I have a few factory subclasses), (3) my SniperFactory class (only showing this subclass in this post), (4) a Survivor class (which holds a survivor's member variables and then (5) my SniperFactory.cpp (implementation of the SniperFactory Class)...
(1) In main I have the following calls and instantiations:
1 2
|
SurvivorFactory* sniperFactory = new SniperFactory();
Survivor* sniper = sniperFactory->createSurvivor("Roger");
|
(2) The base factory class, from which SniperFactory inherits:
1 2 3 4 5 6 7 8 9
|
class SurvivorFactory
{
public:
SurvivorFactory() {}
virtual ~SurvivorFactory() {}
virtual Survivor* createSurvivor(string);
};
|
(3) The SniperFactory Class:
1 2 3 4 5 6 7 8
|
class SniperFactory : public SurvivorFactory
{
public:
SniperFactory() {}
~SniperFactory() {}
Survivor* createSurvivor(string);
};
|
(4) The Survivor class: (Note I created and implemented all the setters(implementation not shown here) so that the SniperFactory implementation can edit the Survivor member variables)
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
|
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; //These functions are implemented and work
virtual void celebrate() = 0; //don't worry about what they do
virtual bool getHit(Zombie*) = 0;
virtual void die() = 0;
void setName(string);
void setPrimary(string);
void setSecondary(string);
void setHP(int);
void setDamage(int);
};
|
Now here is my problem: I have to return a pointer to a Survivor, but I cannot say something such as "Survivor *sniper = new Survivor(name);" - as I currently do in my code. This is because there are pure virtual functions in the Survivor class. How can I work around this as I need to return a pointer to a Survivor to return a Survivor(created by the Sniper Factory) to where the Survivor is instantiated in main.
(5) Here is my implementation of the SniperFactory Class:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Survivor* SniperFactory::createSurvivor(string name)
{
Survivor *sniper = new Survivor(name); //this is resulting in the error
sniper->setName(name);
sniper->setPrimary(".308 Rifle");
sniper->setSecondary("Binoculars");
sniper->setHP(6);
sniper->setDamage(5);
return sniper;
}
|
Help in this matter will be greatly appreciated!!
P.S Please also indicate if this is the right approach (to use setters to change the member variables of the Survivor class) to create a new survivor (I have to do it through a factory).
Thanks!