我正在尝试使用另一个类的函数,但我的依赖似乎阻止了我。
的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 type
和expected ')' before '&' token StateMachine(Gesture&);
任何人都可以弄清楚发生了什么事吗?我无法将我的功能移至gesture.cpp
,因为它使用存储在statemachine.hpp
答案 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;