编辑:对于后人这是我正在做的事情,事实证明它正在工作,我只是因为某些原因将rocketsystem指针设置为null
class Vector2D
{
public:
float x;
float y;
Vector2D(float x_, float y_) :x(x_),y(y_){}
};
class System
{
protected:
vector<Vector2D> position;
public:
void addEntity(Vector2D newPos)
{
position.push_back(newPos);
}
};
class projectile :public System
{
public:
void createRocket(Vector2D pos)
{
addEntity(pos);
}
};
class player : public System
{
public:
projectile* rocketSystem;
void init(projectile* rocketsys){rocketSystem = rocketsys;}
void fireRocket(Vector2D pos)
{
rocketSystem->createRocket(pos);
}
};
int main (int argc, char * const argv[])
{
player* PlayerSystem = new player;
projectile* RocketSystem = new projectile;
PlayerSystem->init(RocketSystem);
PlayerSystem->fireRocket(Vector2D(0,0));
return 0;
}
答案 0 :(得分:1)
我将使用我的心灵力量并猜测System类没有createRocket()
成员。由于player系统具有System *rocketSystem
而不是rocketsystem *rocketSystem
,因此可以在rocketSystem
成员上调用的唯一函数是在System类中声明的函数。如果您希望能够调用该函数,则它必须是rocketsystem*
,并且必须在定义playersystem::fireRocket
函数之前声明rocketsystem类。