10

I know that similar questions to this have been asked before but after doing my research I still have questions about circular header includes.

//FooA.h
#ifndef H_FOOA
#define H_FOOA

#include "foob.h"

class FooA{
   public:
      FooB *fooB;
};


//FooB.h
#ifndef H_FOOB
#define H_FOOB

class FooA;
class FooB{
   public:
      FooA *fooA;
};

Now if I have two circular dependencies this is the way that I have seen people on stackoverflow get around the problem. My only problem with this is that in my main.cpp I must include fooa.h first and then foob.h

//main.cpp the right way
#include "fooa.h"
#include "foob.h"

//main.cpp that will surely get a compile error
#include "foob.h"
#include "fooa.h"

Now my question is "Is there a way to forward declare these classes in a way that will allow me to not care about the order in which I include the header files in my main.cpp?"

2
  • As the answers have pointed out, you can use a forward declaration in both files. If the situation were more complicated, you could #include "fooa.h" from foob.h, which would guarantee the include order you want.
    – JoeG
    Aug 15, 2012 at 14:48
  • There's nothing circular in the includes in the example code. If you add the missing #endif's everything is fine. What compile error are you getting? Aug 15, 2012 at 15:10

4 Answers 4

16

Is there a way to forward declare these classes in a way that will allow me to not care about the order in which I include the header files in my main.cpp?

since you are dealing with simple pointers only, you can use a forward declaration here in both cases:

FooA.h

#ifndef H_FOOA
#define H_FOOA

// #include "foob.h" << not needed!
class FooB;       // << substitute with a forward declaration of FooB

class FooA{
   public:
      FooB *fooB;
};
#endif

FooB.h

#ifndef H_FOOB
#define H_FOOB

class FooA;
class FooB{
   public:
      FooA *fooA;
};
#endif
4
  • @user1600812 exactly, and main can include the files it needs -- using this example, order will not matter.
    – justin
    Aug 15, 2012 at 14:49
  • I guess I forgot to mention that in my specific code. fooa.h forward declares itself because there are a couple of self referring pointers in my code. In my specific case I don't think you could do it your way because I would be forward declaring a class twice. However in the example I made above your solution works. Thankyou. Aug 15, 2012 at 14:55
  • @user1600812 C++ permits multiple forward declarations (e.g. in separate files).
    – justin
    Aug 15, 2012 at 15:06
  • @user1600812 - there's no problem with multiple forward declarations in the same file as long as they're consistent. Aug 15, 2012 at 15:07
3

You don't have to care about the order because fooa.h includes foob.h, and foob.h has the forward declaration for FooA. Everything is correct already in your code.

1
  • 1
    Modulo the missing #endif's, there's no problem at all with the code as posted. There's no circularity, either, which makes me suspect that there's something missing. Aug 15, 2012 at 15:09
1

As both classes simply contain a pointer, you don't need to include the other header. A forward declaration will do.

Any code that actually uses the other class will need the header, but that should be in a cpp file.

1

Use a forward declare in both header files.

Did you know you can declare it on the same line?

In "FooB.h"

class FooB{
    public:
        class FooA *fooA;
};

In "FooA.h"

class FooA {
    public:
        class FooB *fooB;
};
1
  • won't that make forward references to FooB::FooA and FooA::FooB respectively?
    – Tom Tanner
    Aug 15, 2012 at 14:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.