C++:工厂设计,无法在矢量上push_back
C++: factory design, can't push_back on vector
我目前正在为一个学校项目设计一个用c++编写的电子游戏。我们被建议使用抽象工厂设计来创建对象。
当玩游戏时,我想将对象(玩家,敌人,武器等)存储在一个实体(超类)指针向量中。
我在类GameLogic中定义关卡,但当我尝试用派生类填充矢量时,有时会出现错误,有时不会。在这个例子中,我添加了一些敌人,加速道具和生命值道具。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);
}
gamelogics .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
。
相关文章:
- 推导 std::vector::back() 的返回类型
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- vector.push_back(vector.back()+1) 是未定义的行为吗?
- C++自己的对象堆栈.在 push() 上复制了什么?
- C++ push() 和 pop() 方法使用指针的动态 LinkedList 的问题
- 如何像在javascript中一样"push" c ++映射
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- std::vector using back(), pop_back(), push_back(), 得到'double free or corruption'错误
- vector.back()和vector.end()有什么区别
- 列表大小为 1,但 front() 和 back() 不相等
- 返回C++中没有 back() 方法的容器的最后一个元素?
- 在非空 std::list 上使用 std::list.back<int>() 时"Segmentation Fault"
- 模板堆栈类的复制构造函数中的 pop() 和 push() 不起作用
- 我们可以使用Back() - 值索引在其上执行向量插入
- 当我运行以下代码添加str.front() str.back时,它给了我200个,但为什么
- push(ing)back在循环中对象指针
- 在 C++ 中将元素添加到空向量:为什么 push.back 有效而 [] 无效
- 我似乎无法让 v.push.back() 处理字符串
- String::push_back() does not push back
- 向量resize(), push.back(), reserve()方法