Headers Including Each Other in C++

C header file loops

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.

C - Headers including each other

Just add a forward declaration, like this in game.h before the function,

typedef struct Level Level;

since it's just a pointer to Level this will do it.

c++ header files including each other mutually

You cannot have each class have "a field that is type of other class"; that would be a recursive definition and not only the compiler would not be able to make any sense out of it, it does not even make logical sense.

Each class having a field that is type of the other class is the kind of impossibility that you only see in M.C. Escher drawings, or animations thereof, like this one:

 

                                            based on Escher's "Print Gallery" Lithograph, 1956

                 B. de Smit and H. W. Lenstra - Source: escherdroste.math.leidenuniv.nl

                     based on Escher's "Print Gallery" Lithograph, 1956, see Wikipedia

 

One of the two fields will have to be a pointer, so as to break the recursive containment, and avoid the logical impossibility.

Which brings us to the next problem: if class B is to contain an instance of class A, then obviously, A has to be declared before class B, so that A is already known to the compiler when compiling B. But if class A is declared before class B, how can we declare a pointer to B in A? Class B is not known yet at the time that A is compiled! The answer to this is a special construct known as forward declaration which exists precisely in order to accommodate situations like this. A forward declaration of class B looks like this:

class B;

All it is telling the compiler is that there will be a class called B. It does not tell the compiler anything about the contents of class B, so there is very little we can do with it, but we can do one thing: declare pointers to B.

So, the full solution to the problem looks like this:

file "A.h":

/* This is called a "forward declaration".  We use it to tell the compiler that
the identifier "B" will from now on stand for a class, and this class will be
defined later. We will not be able to make any use of "B" before it has been
defined, but we will at least be able to declare pointers to it. */
class B;

class A
{
/* We cannot have a field of type "B" here, because it has not yet been
defined. However, with the forward declaration we have told the compiler
that "B" is a class, so we can at least have a field which is a pointer
to "B". */
B* pb;
}

file "B.h":

#include "A.h"

class B
{
/* the compiler now knows the size of "A", so we can have a field
of type "A". */
A a;
}

Headers Including Each Other in C++

By "the macros" I assume you mean the #ifndef include guards?
If so, #includes should definitely go inside. This is one of the major reasons why include guards exists, because otherwise you easily end up with an infinite recursion as you noticed.

Anyway, the problem is that at the time you use the A and B classes (inside the other class), they have not yet been declared. Look at what the code looks like after the #includes have been processed:

//#include "A.h" start
#ifndef A_H_
#define A_H_

//#include "B.h" start
#ifndef B_H_
#define B_H_

//#include "A.h" start
#ifndef A_H_ // A_H_ is already defined, so the contents of the file are skipped at this point
#endif /*A_H_*/

//#include "A.h" end

class B
{
private:
A& a;

public:
B(A& a) : a(a) {}
};

#endif /*B_H_*/

//#include "B.h" end

class A
{
private:
B b;

public:
A() : b(*this) {}
};

#endif /*A_H_*/
//#include "A.h" end

int main()
{
A a;
}

Now read the code. B is the first class the compiler encounters, and it includes an A& member. What is A? The compiler hasn't encountered any definition of A yet, so it issues an error.

The solution is to make a forward declaration of A. At some point before the definition of B, add a line class A;

This gives the compiler the necessary information, that A is a class. We don't know anything else about it yet, but since B only needs to include a reference to it, this is good enough. In the definition of A, we need a member of type B (not a reference), so here the entire definition of B has to be visible. Which it is, luckily.

Header Files Including each other

Maybe you want just a forward declaration?

// In Integer.h
class Decimal;
class Integer
{
...
operator Decimal();
};

(You missed the last semicolon in your code, by the way.)

C++ header files including each other

You have a circular dependency between Node and model.

To deal with this, instead of...

#include "Node.h"

...in model.h , forward declare...

class Node;

...and this will allow you to have Node& node; in your Model class.

Or vice-versa.

Better still... see if you can revisit your design and eliminate this circular dependency.

I need to include two header files to each other not using forward declaration cause get incomplete type error

You can't include two header files to each other. There should be forward declaration in one of the files and function definition has to be pushed to .cpp file where you can include the header file.

// HeaderA.h file
#ifndef H_A_H
#define H_A_H
#include "HeaderB.h"
class A {
private:
int b;
public:
void function() {
// using methods of B
B b;
b.function();
}
};
#endif

// HeaderB.h file
#ifndef H_B_H
#define H_B_H

class A;

class B {
private:
int a;
public:
void function();
};
#endif

// Main.cpp
#include "HeaderA.h"
#include "HeaderB.h"

void B::function()
{
// using methods of A
A a;
a.function();
}

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

C++ : Headers Including each others causes an Error class has not been declared

To break the circular dependency, you need to add a forward declaration of the depended-on classes to each of your header files, like this:

#ifndef A_H
#define A_H
#include <iostream>

class CRoom;

class CPlay {
...

and:

#ifndef D_H
#define D_H
#include <iostream>

class CPlay;

class CRoom {
...

Note that this allows you to remove the #includes from the header files themselves.

C Some headers include each other

Why do you want to include foo_bazz in foo.h?

It will fail for sure. The inclusion of the foo.h in foo_bazz.h will not declare the type as it is already protected by the guards.

It makes no sense. Simply include foo_bazz.h in your .c file where you will have function and data definition. IMO X-Y problem.



Related Topics



Leave a reply



Submit