Note that there are some explanatory texts on larger screens.

plurals
  1. POHeaders Including Each Other in C++
    primarykey
    data
    text
    <p>I'm a C++ newbie, but I wasn't able to find the answer to this (most likely trivial) question online. I am having some trouble compiling some code where two classes include each other. To begin, should my #include statements go inside or outside of my macros? In practice, this hasn't seemed to matter. However, in this particular case, I am having trouble. Putting the #include statements outside of the macros causes the compiler to recurse and gives me "#include nested too deeply" errors. This seems to makes sense to me since neither class has been fully defined before #include has been invoked. However, strangely, when I try to put them inside, I am unable to declare a type of one of the classes, for it is not recognized. Here is, in essence, what I'm trying to compile:</p> <p>A.h</p> <pre><code>#ifndef A_H_ #define A_H_ #include "B.h" class A { private: B b; public: A() : b(*this) {} }; #endif /*A_H_*/ </code></pre> <p>B.h</p> <pre><code>#ifndef B_H_ #define B_H_ #include "A.h" class B { private: A&amp; a; public: B(A&amp; a) : a(a) {} }; #endif /*B_H_*/ </code></pre> <p>main.cpp</p> <pre><code>#include "A.h" int main() { A a; } </code></pre> <p>If it makes a difference, I am using g++ 4.3.2.</p> <p>And just to be clear, in general, where should #include statements go? I have always seen them go outside of the macros, but the scenario I described clearly seems to break this principle. Thanks to any helpers in advance! Please allow me to clarify my intent if I have made any silly mistakes!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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