0

I have a project I'm working on and I'm currently working on the header files. For simplicity let's say I have two header files so far. fileX.h and fileY.h

The code I have in both is just class definitions. But, fileX needs fileY's class definitions and fileY needs fileX. I tried having inside of fileX a #include "fileY.h" and have a include "fileX.h" inside of my fileY, but I'm getting an error that gets in a never ending loop. How can I fix this ? Thanks

Ahmad Safa
  • 51
  • 6
  • 4
    The search keyword you're looking for is "circular dependency" – Weston McNamara Jan 15 '22 at 04:41
  • https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes – Weston McNamara Jan 15 '22 at 04:41
  • You cannot have `class A` have `class B` as a member and then `class B` to have `class A` as a member at the same time. You can have a (smart)pointer though. For using pointer you do not need to fully define class. you just use forward declaration. – Slava Jan 15 '22 at 04:48

1 Answers1

1

How can I fix this ?

By breaking the circular dependency. Its simply impossible for A to depends on B's definition while also B depending on A's definition. You must get rid of one of the dependencies.

eerorika
  • 232,697
  • 12
  • 197
  • 326