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

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

本文关键字:依赖 #include 我的      更新时间:2023-10-16

我试图使用另一个类中的函数,但我的依赖关系似乎阻止了我。

main.cpp

#include "gesture.hpp"
#include "statemachine.hpp"
Gesture g;
StateMachine sm(g);

手势.hpp

#include "hand.hpp"

状态机.hpp

#include "gesture.hpp"
StateMachine(Gesture&);
Gesture *g;

我正在努力实现的目标:我正在尝试使用已声明的StateMachine sm中的一个函数。这个函数将在gesture.cpp中的函数内部调用,我将给Gesture g类一个指向StateMachine sm的指针。(我会在主声明sm之后这样做)我可以在gesture.cpp文件中使用#include "statemachine.hpp",但我想将其移动到gesture.hpp,这样我就可以将其作为指针存储在该类中作为变量。

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

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

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

有人能搞清楚发生了什么事吗?我无法将函数移动到gesture.cpp,因为它使用了存储在我的statemachine.hpp 中的数组

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

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

statemachine.hpp中没有展开文件"姿态.hpp",因为我认为您已经提供了一些防止循环依赖的保护。所以编译器不知道手势是什么。

为了解决编译错误,您可以向statemachine.hpp提交一个手势的正向声明,如下所示:

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