我的#include依赖似乎阻止我使用类

时间:2015-11-25 00:34:38

标签: c++ dependencies header-files

我正在尝试使用另一个类的函数,但我的依赖似乎阻止了我。

的main.cpp

#include "gesture.hpp"
#include "statemachine.hpp"

Gesture g;
StateMachine sm(g);

gesture.hpp

#include "hand.hpp"

statemachine.hpp

#include "gesture.hpp"

StateMachine(Gesture&);
Gesture *g;

我想要完成的任务: 我正在尝试使用我声明的StateMachine sm函数。这个函数将在gesture.cpp中的函数内部调用,我会给Gesture g类指向StateMachine sm的指针。 (我会在主要内容sm之后执行此操作)我可以在#include "statemachine.hpp"文件中gesture.cpp但我想将其移至gesture.hpp以便我可以使用它作为指针存储为该类中的变量。

所以当我这样做的时候 的 gesture.hpp

#include "hand.hpp"
#include "statemachine.hpp"

我收到错误'Gesture' does not name a typeexpected ')' before '&' token StateMachine(Gesture&);

任何人都可以弄清楚发生了什么事吗?我无法将我的功能移至gesture.cpp,因为它使用存储在statemachine.hpp

中的数组

1 个答案:

答案 0 :(得分:1)

你没有提供细节,所以我会在这里发布我的猜测。 当预编译器分析“gesture.hpp”时,它会将其展开为类似的东西:

codes in hand.hpp
StateMachine(Gesture&);
Gesture *g;

文件“gesture.hpp”未在statemachine.hpp中展开,因为我认为你提供了一些针对循环依赖的保护。所以编译器不知道Gesture是什么。

要解决编译错误,可以将一个Gesture的前向声明放到statemachine.hpp中,如下所示:

class Gesture;
StateMachine(Gesture&);
Gesture *g;