title: Resolve build errors due to circular dependency amongst classes
body: <p>I often find myself in a situation where I am facing multiple compilation/linker errors in a C++ project due to some bad design decisions (made by someone else :) ) which lead to circular dependencies between C++ classes in different header files <em>(can happen also in the same file)</em>. But fortunately(?) this doesn't happen often enough for me to remember the solution to this problem for the next time it happens again.</p>
  
  <p>So for the purposes of easy recall in the future I am going to post a representative problem and a solution along with it. Better solutions are of-course welcome.</p>
  
  <hr>
  
  <ul>
  <li><p><code>A.h</code></p>
  
  <pre><code>class B;
  class A
  {
      int _val;
      B *_b;
  public:
  
      A(int val)
          :_val(val)
      {
      }
  
      void SetB(B *b)
      {
          _b = b;
          _b-&gt;Print(); // COMPILER ERROR: C2027: use of undefined type 'B'
      }
  
      void Print()
      {
          cout&lt;&lt;"Type:A val="&lt;&lt;_val&lt;&lt;endl;
      }
  };
  </code></pre></li>
  </ul>
  
  <hr>
  
  <ul>
  <li><p><code>B.h</code></p>
  
  <pre><code>#include "A.h"
  class B
  {
      double _val;
      A* _a;
  public:
  
      B(double val)
          :_val(val)
      {
      }
  
      void SetA(A *a)
      {
          _a = a;
          _a-&gt;Print();
      }
  
      void Print()
      {
          cout&lt;&lt;"Type:B val="&lt;&lt;_val&lt;&lt;endl;
      }
  };
  </code></pre></li>
  </ul>
  
  <hr>
  
  <ul>
  <li><p><code>main.cpp</code></p>
  
  <pre><code>#include "B.h"
  #include &lt;iostream&gt;
  
  int main(int argc, char* argv[])
  {
      A a(10);
      B b(3.14);
      a.Print();
      a.SetB(&amp;b);
      b.Print();
      b.SetA(&amp;a);
      return 0;
  }
  </code></pre></li>
  </ul>