2011-04-04 68 views
0

我目前正在设计一个学校项目的c + +视频游戏。我们被建议使用抽象工厂来创建我们的对象。C++:工厂设计,不能push_back矢量

当玩游戏时,我想将对象(玩家,敌人,武器等)存储在一个entitie(超类)指针向量中。
我在类GameLogic中定义了自己的级别,但是当我尝试用派生类填充向量时,有时会出错,有时候我不会。
在这个例子中,我添加了一些敌人,speedpowerups和healthpowerup。 SpeedPowerUp和HealthPowerUp是一样的,唯一不同的是名称。
但加入HealthPowerUp作品,添加SpeedPowerUp不工作...

这些都是从我的代码的某些代码段:

东西一般

typedef vector <Entitie*> container; 
typedef vector <Entitie*>::iterator iter; 

继承结构

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(部分代码)

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(一些代码)

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(一些代码)

class GameLogic 
{ 
protected: 
AbstractFactory* factory; 
public: 
GameLogic(AbstractFactory*); 
virtual ~GameLogic(); 

void createLevel(container&, int); 

};

gamelogic.cpp(一些代码)

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)); 
} 


} 

编译错误:

../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 
+0

SpeedPowerUp是否从Entitie派生? – zdan 2011-04-04 17:20:20

+0

是的。我将继承结构添加到原始帖子中。 – CrazyNilus 2011-04-04 17:46:39

回答

1

您正尝试将SpeedPowerUp增加的Entitie向量。你可能想确保你的SpeedPowerUp实际上是从Entitie继承的。

+0

我跟进了我的代码,并且speedpowerup继承了powerup,它从entitie继承。例如此代码编译(不能使速度上电,因为它包含纯虚函数): \t SDLSpeedPowerUp * test = new SDLSpeedPowerUp(0,0,0,factory); \t entities.push_back(test); – CrazyNilus 2011-04-04 17:31:41

+0

@CrazyNilus:您应该将层次结构的这些位添加到问题中,因为它比特定错误的其他逻辑更有趣。另外,虽然您可能完全确定继承关系存在,但编译器不相信它。继承是公共的吗? – 2011-04-04 17:36:30

+0

将继承定义为public。我将在帖子中添加继承结构。 – CrazyNilus 2011-04-04 17:42:10