I have a struct called CardState defined in Application.h:
#ifndef APPLICATION_H
#define APPLICATION_H
#include <Session.h> // Note that both files include each others
struct CardState {
bool property1;
bool property2;
};
class Application : public QApplication {
Q_OBJECT
public:
explicit Application(int argc, char *argv[]);
}
I then use this CardState type in a different file:
#ifndef SESSION_H
#define SESSION_H
#include <Application.h>
struct CardState;
class Session : public QObject {
Q_OBJECT
public:
void setCardState(const CardState& cardState);
CardState cardState() const;
private:
CardState cardState_;
};
}
#endif // SESSION_H
At the time Session.h is included it seems that Application.h has not yet been included so I need to forward declare the struct (see above). However, this is not enough, after having added the forward declaration, I still get this error:
'Session::cardState_' uses undefined struct 'CardState'
Which, if I understand correctly, means that the compiler doesn’t know how to initialize my variable since CardState is only partially declared. I know I can fix this by making CardState a pointer, and that’s usually what I do, but is there any other, more proper way, to fix this?
As per Oli Charlesworth’s comment, this should work as it is.
In general case, you would want to either:
CardStatein its own header and then #include it in all “client” headers, thus avoiding any complications that might arise from include order of these headers.Session::cardState_as a (smart) pointer and avoid need for actualCardStatedeclaration.— EDIT —
Now that you edited the code, it is clear that there are circular include dependencies after all, in which case your code really isn’t going to work, so you must apply one of the solutions above.