Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;:工厂设计,can';t将_推回向量 我目前正在为一个学校项目设计一个C++游戏。我们被建议使用抽象工厂设计来创建对象_C++ - Fatal编程技术网

C++;:工厂设计,can';t将_推回向量 我目前正在为一个学校项目设计一个C++游戏。我们被建议使用抽象工厂设计来创建对象

C++;:工厂设计,can';t将_推回向量 我目前正在为一个学校项目设计一个C++游戏。我们被建议使用抽象工厂设计来创建对象,c++,C++,玩游戏时,我希望将对象(玩家、敌人、武器等)存储在一个实体(超类)指针向量中。 我在类GameLogic中定义了我的级别,但当我尝试用派生类填充向量时,有时会出错,有时不会。 在这个例子中,我添加了一些敌人,加速加电和健康加电。SpeedPowerUp和HealthPowerUp是相同的,只是名称不同。 但是添加一个HealthPowerUp有效,添加一个SpeedPowerUp无效 以下是我的代码中的一些片段: 一般的东西 typedef vector <Entitie*> con

玩游戏时,我希望将对象(玩家、敌人、武器等)存储在一个实体(超类)指针向量中。
我在类GameLogic中定义了我的级别,但当我尝试用派生类填充向量时,有时会出错,有时不会。
在这个例子中,我添加了一些敌人,加速加电和健康加电。SpeedPowerUp和HealthPowerUp是相同的,只是名称不同。
但是添加一个HealthPowerUp有效,添加一个SpeedPowerUp无效

以下是我的代码中的一些片段:

一般的东西

typedef vector <Entitie*> container;
typedef vector <Entitie*>::iterator iter;
sdlfactory.h(一些代码)

sdlfactory.cpp(一些代码)

gamelogic.h(一些代码)

})

gamelogic.cpp(一些代码)

void GameLogic::createLevel(容器和实体,常量int级别){
srand(时间(空));
//树敌
int x=0;
整数产卵率=1000-25*级;
而((x+=rand()%(繁殖率))创造了敌人(x,y,最大生命值,速度,加速度,刹车速度,伤害,waeponspeed));
}
x=0;
而((x+=rand()%sprownrate)createSpeedPowerUp(x,y,effect));
}
x=0;
而((x+=rand()%spownRate)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
。/GameLogic.cpp:在成员函数“void CDS::GameLogic::createLevel(CDS::container&,int)”中:
../GameLogic.cpp:39:错误:调用“std::vector::push_back(CDS::SpeedPowerUp*)”时没有匹配的函数
/usr/include/c++/4.0.0/bits/stl_vector.h:602:注:候选项为:void std::vector::push_back(const _Tp&)[带_Tp=CDS::entiie*,_Alloc=std::allocator]
make:**[GameLogic.o]错误1

您正在尝试向
实体的向量添加
加速通电
。您可能想确保您的
SpeedPowerUp
实际上继承自
Entitie

SpeedPowerUp是否源自Entitie?是的。我在原始文章中添加了继承结构。我继续我的代码,并加速了powerup继承自powerup,后者继承自entitie。e、 g.此代码编译(无法进行加速通电,因为它包含纯虚拟函数):sdlspeedpowerrup*test=new-sdlspeedpowerrup(0,0,0,工厂);实体。推回(测试)@CrazyNilus:您应该将层次结构的这些部分添加到问题中,因为对于特定错误,它比其他逻辑部分更有趣。此外,虽然您可能完全确定继承关系存在,但编译器并不相信它。继承是公共的吗?继承定义为公共的。我将在帖子中添加继承结构。这也是我的结论。。。糟糕的是,这不是答案。由于SpeedPowerUp和HealthPowerUp是相同的,所以您可能希望将HealthPowerUp作为通电使用。如果他们有不同的时间或福利价值,您可以使用您的工厂根据需要分配这些价值。另一种方法是使PowerUp类成为通用的aka模板。。。在这种情况下,您仍然需要使用工厂来正确分配它。如果您确实需要或想要将它们分开,尽管您可能只需要调试bejesus,就像往常一样,这只是一个小错误。我忘了包括SpeedPowerUp.h和HealthPowerUp.h。我不知道为什么它和HealthPowerUp一起工作,但我现在知道了,现在它工作了。
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);
...
};
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);
}
class GameLogic 
{
protected:
AbstractFactory* factory;
public:
GameLogic(AbstractFactory*);
virtual ~GameLogic();

void createLevel(container&, int);
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