Search code examples
c++header-files

How to resolve this header include loop?


Hi I already read similar questions about this topic, but I coudn't resolve my problem. I think I have to do a forward declaration so I tried the following.

I have three classes A, B and InterfaceA

Defintion InterfaceA

#ifndef INTERFACE_A_H
#define INTERFACE_A_H
#include "B.h"

namespace Example
{
  class B; // Forward declaration?
  class InterfaceA
  {
     Example::B test;

   };

}

#endif 

Definiton class A

#ifndef A_H
#define A_H
#include "InterfaceA.h"
namespace Example
{
  class A : public Example::InterfaceA
  {

  };
}
#endif

Defintion class B

#ifndef B_H
#define B_H
#include "A.h"

namespace Example
{
  class A; // Forward declaration?
  class B
  {
     Example::A test;
  };

}

#endif

main

#include "A.h"
#include "B.h"
int main()
{
  Example::A a;
  Example::B b;
}

I get the following error in visual studio:

'Example::B::test' uses undefined class 'Example::A'

Edit: Thank you so far, for all the help. It was very helpful. I think my problem was that I had a very poor design in my real project. I will change that.

Beside that I have now a better understanding for forward declarations :-)


Solution

  • You are creating circular dependency. Revise your design.

    Do you really need an instance of class A inside B and B inside A?