37

I'm new to C++, so this question may be basic:

I have two classes that need to refer to each other. Each is in its own header file, and #include's the other's header file. When I try to compile I get the error "ISO C++ forbids declaration of ‘Foo’ with no type" for one of the classes. If I switch things so the opposite header gets parsed first I get the same error with the other class.

Is it possible in C++ to have two classes that need references to each other?

For more detail: I have an "App" class and a "Window" class. App needs to refer to Window to make the window. Window has a button that calls back to App, so it needs a reference to App. If I can't have two classes refer to each other, is there a better way to implement this?

  • 2
    It should be noted that mutual dependencies should be avoided when possible. Would it make sense to factor the functionality that buttons calls to in the app into a separate controller-ish object? – Greg D Jun 15 '09 at 01:59
  • 1
    I find this problem quite often in my small games. I have a World object containing Game objects that need to reference the World object for e.g. Collision detection. Haven't found a good way to refactor that. (And btw, waking up a 7 years old discussion aught to earn me some sort of badge ) – Johan Sep 09 '16 at 17:04

2 Answers2

59

You can use forward declarations in the header files to get around the circular dependencies as long as you don't have implementation dependencies in the headers. In Window.h, add this line:

class App;

In App.h, add this line:

class Window;

Add these lines before the class definitions.

Then in the source files, you include the headers for the actual class definitions.

If your class definitions reference members of the other class (for example, in inlines), then they need to be moved to the source file (no longer inline).

sean e
  • 11,792
  • 3
  • 44
  • 56
  • 5
    I cannot over-emphasize how important it is to include the header files with the actual definition in the **source** files and not the header files. I've had the forward declarations, but I've been running into "redefinition errors" until I've read this. What a brain fart really.. Anyways, thank you! – gstukelj Jun 08 '20 at 14:56
  • 1
    ^ This. This saved me from all this circular dependency trouble. – Mark Jeronimus Nov 19 '21 at 09:26
13

Forward declaration is the way to go.

If you are using pointers\reference in class header then Forward declaration at both sides would work for you.

If you are creating the object as a class member then you must include header itself. ( Forward declaration won't work as compiler needs class definition for knowing the size).

Refer C++ FAQ for solving such senario:

If you are creating the Window as member then include the Window header in App but at the same time Window shouldn't include the App's header. Use the combination of pointer to App and the forward declaration there.

ניר
  • 1,204
  • 1
  • 8
  • 28
aJ.
  • 34,624
  • 22
  • 86
  • 128