0

I need to include recursively class header files.

"Foo.h"

#ifndef FOO_H
#define FOO_H
#include "Bar.h"

class Foo {
public:
    Bar* barMember;
};
#endif

"Bar.h"

 #ifndef BAR_H
 #define BAR_H
 #include "Foo.h"
 class Bar {
 public:
     Foo* fooMember;
 };
 #endif

In this case I am getting errors like

'class' does not name a type

Consider that in this case Foo is the main class that includes a lot of other classes as a members. But with one member I need to have bidirectional connection.

So why do I have such problems?

Deduplicator
  • 41,806
  • 6
  • 61
  • 104
ahguobrf
  • 85
  • 1
  • 6

1 Answers1

4

Use forward declarations:

#ifndef FOO_H
#define FOO_H

class Bar;

class Foo
{
public:
    Bar* barMember;
};
#endif

and:

#ifndef BAR_H
#define BAR_H

class Foo;

class Bar
{
public:
    Foo* fooMember;
};
#endif

You will only need to include the respective header files in the .cpp files containing the implementations, so there won't be mutual inclusions.

SirDarius
  • 36,426
  • 7
  • 79
  • 95
  • Huh, thanks .. You really saved my hours, I am so exhausted ... Thx again – ahguobrf Dec 05 '14 at 00:11
  • Oh, no , again , I don't know what to it gives me a lot of errors. Like Forward declaration ................. I don't know what to do ... – ahguobrf Dec 05 '14 at 01:21