views:

1599

answers:

9

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;

element.h:
#include "tree.h"

typedef struct element_
{
    tree *tree_parent;
    char *name;
    ...
} element;

The problem I have is, that they both reference each other, so tree needs element included, and element needs tree included.

This doesn't work, to initialise the 'tree' structure, the element structure must be known about, but to initialise the element structure, the tree structure must be known about.

How do you resolve these types of loops (I think it has some thing to do with 'forward declaration'?)?

+3  A: 

The correct answer is to use include guards, and to use forward declarations.

Include Guards

/* begin foo.h */
#ifndef _FOO_H
#define _FOO_H

// Your code here

#endif
/* end foo.h */

Visual C++ also supports #pragma once. It is a non standard preprocessor directive. In exchange for compiler portability, you reduce the possibility of preprocessor name collisions and increase readability.

Forward Declarations

Forward declare your structs. If the members of a struct or class are not explicitly needed, you can declare their existence at the beginning of a header file.

struct tree;    /* element.h */
struct element; /* tree.h    */
sludge
+5  A: 

Crucial observation here is that the element doesn't need to know the structure of tree, since it only holds a pointer to it. The same for the tree. All each needs to know is that there exists a type with the relevant name, not what's in it.

So in tree.h, instead of:

#include "element.h"

do:

typedef struct element_ element;

This "declares" the types "element" and "struct element_" (says they exist), but doesn't "define" them (say what they are). All you need to store a pointer-to-blah is that blah is declared, not that it is defined. Only if you want to deference it (for example to read the members) do you need the definition. Code in your ".c" file needs to do that, but in this case your headers don't.

Some people create a single header file which forward-declares all the types in a cluster of headers, and then each header includes that, instead of working out which types it really needs. That's neither essential nor completely stupid.

The answers about include guards are wrong - they're a good idea in general, and you should read about them and get yourself some, but they don't solve your problem in particular.

Steve Jessop
+2  A: 

Read about forward declarations.

ie.


// tree.h:
#ifndef TREE_H
#define TREE_H
struct element;
struct tree
{
    struct element *obj;
    ....
};

#endif

// element.h:
#ifndef ELEMENT_H
#define ELEMENT_H
struct tree;
struct element
{
    struct tree *tree_parent;
    ...
};
#endif
Greg Rogers
I believe that if you include both tree.h and element.h (in that sequence), then the tree structure in element.h is different from the one in tree.h - the plain 'struct tree' loses the internal structure information of the tree in tree.h.
Jonathan Leffler
+17  A: 

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:

// tell the compiler that element is a structure typedef:
typedef struct element_ element;

typedef struct tree_ tree;
struct tree_
{
    tree *first_child;
    tree *next_sibling;
    int tag;

    // now you can declare pointers to the structure.
    element *obj;
};

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.

Nils Pipenbrinck
A: 

Include guards are useful, but don't address the poster's problem which is the recursive dependency on two data structures.

The solution here is to declare tree and/or element as pointers to structs within the header file, so you don't need to include the .h

Something like:

struct element_;
typedef struct element_ element;

At the top of tree.h should be enough to remove the need to include element.h

With a partial declaration like this you can only do things with element pointers that don't require the compiler to know anything about the layout.

Rob Walker
A: 
dmeister
A: 

I agree with the last answer.

Sachin Chourasiya
+1  A: 

Forward declaratio is the way with which you can guarantee that there will be a tyoe of structure which will be defined later on.

Sachin Chourasiya