我有2个具有循环依赖项的类:
GameState.h
#include "Ball.h"
class GameState{
Ball ball;
public:
//...
void groundTouched();
};
Ball.h
#include "GameState.h"
class Ball{
public:
//...
void update(GameState* gameState);
};
Ball.cpp
#include "Ball.h"
void Ball::update(GameState* gameState){
gameState->groundTouched();
}
当然还有错误,例如:
Ball.h:34:69: error: ‘GameState’ has not been declared
然后我在这里使用了前向声明:
class GameState;
class Ball{
public:
//...
void update(GameState* gameState);
};
但出现错误:
Ball.cpp:51:22: error: invalid use of incomplete type ‘class GameState’
如何从Ball调用GameState方法?可能吗?我知道我可以从Ball中删除GameState.h并从GameState中执行ball.isGroundTouched()之类的操作,但对我而言,第一种选择看起来更面向对象,并且更受青睐。
答案 0 :(得分:4)
您仍必须在 source 文件GameState.h
中包含Ball.cpp
头文件。