I have a couple of header files, which boil down to:
tree.h:
#include 'element.h' typedef struct tree_ { struct *tree_ first_child; struct *tree_ next_sibling; int tag; element *obj; .... } tree;
and element.h:
#include 'tree.h' typedef struct element_ { tree *tree_parent; char *name; ... } element;
The problem is that they both reference each other, so tree needs element to be included, and element needs tree to be included.
This doesn’t work because to define the ‘tree’ structure, the element structure must be already known, but to define the element structure, the tree structure must be known.
How to resolve these types of loops (I think this may have something to do with ‘forward declaration’?)?
I think the problem here is not the missing include guard but the fact that the two structures need each other in their definition. So it’s a type define hann and egg problem.
The way to solve these in C or C++ is to do forward declarations on the type. If you tell the compiler that element is a structure of some sort, the compiler is able to generate a pointer to it.
E.g.
Inside tree.h:
That way you don’t have to include element.h inside tree.h anymore.
You should also put include-guards around your header-files as well.