C++:工厂设计,不能在 vector 上 push_back

标签 c++

我目前正在为一个学校项目设计一个使用 C++ 的视频游戏。我们被建议使用抽象工厂设计来创建我们的对象。

玩游戏时,我想将对象(玩家、敌人、武器等)存储在一个实体(父类(super class))指针 vector 中。
我在类 GameLogic 中定义关卡,但是当我尝试用派生类填充 vector 时,有时会出错,有时不会。
在这个例子中,我添加了一些敌人、加速 Prop 和健康 Prop 。 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 的 vector 中。您可能想确保您的 SpeedPowerUp 实际上是从 Entitie 继承的。

关于C++:工厂设计,不能在 vector 上 push_back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5541935/

相关文章:

c++ - 在运行时将 Qt 小部件添加到 MainWindow

c++ - 如何制作一个易于通过引用进行比较的类型

c++ - 在 C++ 中使用函数对数组进行排序

c++ - 显示二维动态数组

c++ - 什么时候加入 Haskell 线程?

c++ - 用cmake构建比特币

c++ -/Fe(名称 EXE 文件)不工作

c++ - 为什么两个连续的收集指令比等效的基本操作执行得更差?

c++ - 将指针转换为数组(int* 到 int[2])

c++ - 为大无符号整数分配内存的有效方法