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

My #include dependencies seem to be preventing me from using a class

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

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 class 一个指向 StateMachine sm 的指针。 (我会在主文件中声明 sm 后执行此操作)我可以在我的 gesture.cpp 文件中 #include "statemachine.hpp" 但我想将其移动到 gesture.hpp 这样我就可以拥有它作为在 class.

中作为变量存储的指针

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

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

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

谁能知道这是怎么回事?我无法将我的函数移动到 gesture.cpp,因为它使用存储在我的 statemachine.hpp

中的数组

您没有提供详细信息,所以我 post 在这里猜测一下。 当预编译器分析 "gesture.hpp" 时,它会将其展开为如下内容:

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

文件 "gesture.hpp" 未在 statemachine.hpp 中展开,因为我认为您已经提供了一些防止循环依赖的保护措施。所以编译器不知道 Gesture 是什么。

为了解决编译错误,可以把Gesture的前置声明放到statemachine.hpp,像这样:

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