簡體   English   中英

C ++:工廠設計,不能在向量上回推

[英]C++: factory design, can't push_back on vector

我目前正在為學校項目設計C ++視頻游戲。 建議我們使用abstractfactory設計來創建我們的對象。

在玩游戲時,我想將對象(玩家,敵人,武器等)存儲在一個完整的(超類)指針向量中。
我在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

您嘗試將添加SpeedPowerUp到的矢量Entitie 您可能要確保SpeedPowerUp實際上是從Entitie繼承的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM