I am currently designing a video game in c++ for a school project. We were advised to use an abstractfactory desing to create our objects.
When playing the game I’d like to store the objects (player, enemy, weapons etc.) in one vector of entitie (superclass) pointers.
I define my levels in a class GameLogic, but when I try to fill the vector with derived classes, I sometimes get error, sometimes I don’t.
In this example, I add some enemies, speedpowerups and healthpowerup. SpeedPowerUp and HealthPowerUp are the same, only difference is the name.
But adding a HealthPowerUp works, adding a SpeedPowerUp doesn’t work…
These are some snippets from my code:
something general
typedef vector <Entitie*> container;
typedef vector <Entitie*>::iterator iter;
Inheritance structure
class SDLSpeedPowerUp: public CDS::SpeedPowerUp....
class SpeedPowerUp: public CDS::PowerUp....
class PowerUp: public CDS::Entitie....
class SDLHealthPowerUp: public CDS::HealthPowerUp....
class HealthPowerUp: public CDS::PowerUp....
class SDLEnemy: public CDS::Enemy....
class Enemy: public CDS::Vehicle....
sdlfactory.h (some code)
class SDLFactory: public CDS::AbstractFactory {
public:
SDLFactory();
virtual ~SDLFactory();
...
Enemy* createEnemy(int,int,int,int,int,int,int,int);
...
HealthPowerUp* createHealthPowerUp(int,int,int);
SpeedPowerUp* createSpeedPowerUp(int,int,int);
...
};
sdlfactory.cpp (some code)
Enemy* SDLFactory::createEnemy(int x,int y,int maxHealth,int maxSpeed,int acceleration,int brakeSpeed,int damage,int bulletSpeed)
{
Waepon* loadedWaepon=createWaepon(x,y,damage,bulletSpeed);
return new SDLEnemy(x,y,maxHealth,maxSpeed,acceleration,brakeSpeed,loadedWaepon,this);
}
HealthPowerUp* SDLFactory::createHealthPowerUp(int x,int y,int effect){
return new SDLHealthPowerUp(x,y,effect,this);
}
SpeedPowerUp* SDLFactory::createSpeedPowerUp(int x,int y,int effect){
return new SDLSpeedPowerUp(x,y,effect,this);
}
gamelogic.h (some code)
class GameLogic
{
protected:
AbstractFactory* factory;
public:
GameLogic(AbstractFactory*);
virtual ~GameLogic();
void createLevel(container&, int);
};
gamelogic.cpp (some code)
void GameLogic::createLevel(container& entities,const int level){
srand(time(NULL));
//create enemies
int x=0;
int spawnRate=1000-25*level;
while((x+=rand()%(spawnRate))<MAXHEIGHT){
int y=rand()%MAXRIGHT;
int maxHealth=rand()%(100+10*level);
int speed=50+rand()%75;
int acceleration=5+rand()%10;
int brakeSpeed=10+rand()%15;
int damage=25+rand()%(15*level);
int waeponspeed=150+rand()%(150);
entities.push_back(factory->createEnemy(x,y,maxHealth,speed,acceleration,brakeSpeed,damage,waeponspeed));
}
x=0;
while((x+=rand()%spawnRate)<MAXHEIGHT){
int y=rand()%MAXRIGHT;
int effect=50+rand()%50;
//the following line of code gives me a compile error
entities.push_back(factory->createSpeedPowerUp(x,y,effect));
}
x=0;
while((x+=rand()%spawnRate)<MAXHEIGHT){
int y=rand()%MAXRIGHT;
int effect=50+rand()%50;
entities.push_back(factory->createHealthPowerUp(x,y,effect));
}
}
compile error:
../GameLogic.cpp: In member function 'void CDS::GameLogic::createLevel(CDS::container&, int)':
../GameLogic.cpp:39: error: no matching function for call to 'std::vector<CDS::Entitie*, std::allocator<CDS::Entitie*> >::push_back(CDS::SpeedPowerUp*)'
/usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = CDS::Entitie*, _Alloc = std::allocator<CDS::Entitie*>]
make: *** [GameLogic.o] Error 1
You are trying to add a
SpeedPowerUpto a vector ofEntitie. You probably want to make sure yourSpeedPowerUpactually inherits fromEntitie.