-2

I am trying to use a function from another class but my dependencies seem to be stopping me.

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;

What I am trying to accomplish: I am trying to use a function from the StateMachine sm that I have declared. This function would be called inside a function in gesture.cpp and I would be giving my Gesture g class a pointer to StateMachine sm. (I would do this after I declare sm in main) I am able to #include "statemachine.hpp" in my gesture.cpp file but I want to move it to gesture.hpp so I can have it as a pointer that is stored as a variable in that class.

So when I do gesture.hpp

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

I get the error 'Gesture' does not name a type and expected ')' before '&' token StateMachine(Gesture&);

Can anyone figure out what is going on? I can't move my function to gesture.cpp because it uses an array that is stored in my statemachine.hpp

wprins
  • 846
  • 2
  • 19
  • 35
  • Possible duplicate of [Purpose of Header guards](http://stackoverflow.com/questions/2979384/purpose-of-header-guards) – Pradhan Nov 25 '15 at 00:37
  • Hard to say since you're not giving enough info to be definitive, but my guess is you need a forward declaration. Look it up. – Edward Strange Nov 25 '15 at 00:39
  • 1
    The problem is probably related to a circular dependency. If `A.h requires B.h` and `B.h` requires `A.h` then you got a problem. To solve it you must use forward declarations, so that `A.h` includes `B.h`, `B.h` doesn't include `A.h` but forward declares the types that are defined in `A.h`. Take a look at: http://stackoverflow.com/questions/4816698/avoiding-circular-dependencies-of-header-files – Jack Nov 25 '15 at 00:40
  • 1
    Actually, I'm taking note of the fact that `Gesture` is nowhere declared or defined in your stated content of `gesture.hpp`. This would explain your problem quite well. – Edward Strange Nov 25 '15 at 00:44
  • Thanks for the responses guys, I have declared ```Gesture();``` in gesture.hpp and the problem actually stems from the fact that I didn't know what forward declaration was, or that it was an option. – wprins Nov 25 '15 at 14:56

1 Answers1

1

You didn't provide the details so i will post my guess here. When the precompiler analyze "gesture.hpp", it will unroll it to something like this:

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

the file "gesture.hpp" is not unrolled in statemachine.hpp because i think you had provided some protections against circular dependency. so the compiler don't know what Gesture is.

To solve the compiling error, you can put a forward declaration of Gesture to statemachine.hpp, like this:

class Gesture;
StateMachine(Gesture&);
Gesture *g;
nason
  • 26
  • 2