Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>By "the macros" I assume you mean the #ifndef include guards? If so, #includes should definitely go inside. This is one of the major reasons why include guards exists, because otherwise you easily end up with an infinite recursion as you noticed.</p> <p>Anyway, the problem is that at the time you use the A and B classes (inside the other class), they have not yet been declared. Look at what the code looks like after the #includes have been processed:</p> <pre><code>//#include "A.h" start #ifndef A_H_ #define A_H_ //#include "B.h" start #ifndef B_H_ #define B_H_ //#include "A.h" start #ifndef A_H_ // A_H_ is already defined, so the contents of the file are skipped at this point #endif /*A_H_*/ //#include "A.h" end class B { private: A&amp; a; public: B(A&amp; a) : a(a) {} }; #endif /*B_H_*/ //#include "B.h" end class A { private: B b; public: A() : b(*this) {} }; #endif /*A_H_*/ //#include "A.h" end int main() { A a; } </code></pre> <p>Now read the code. B is the first class the compiler encounters, and it includes an <code>A&amp;</code> member. What is <code>A</code>? The compiler hasn't encountered any definition of <code>A</code> yet, so it issues an error.</p> <p>The solution is to make a forward declaration of A. At some point before the definition of B, add a line <code>class A;</code></p> <p>This gives the compiler the necessary information, that A is a class. We don't know anything else about it yet, but since B only needs to include a reference to it, this is good enough. In the definition of A, we need a member of type B (not a reference), so here the entire definition of B has to be visible. Which it is, luckily.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload