title: Accessing a nested class member from another nested class (C++)
body: <p>I have a class <code>Enclosing</code>, that contains <code>PImpl</code> class.
  The declaration of the <code>Enclosing</code> is in <code>all_includes.h</code> file and it's definition is in <code>enclosing.cpp</code>. I can not change that. <code>PImpl</code> is defined in <code>enclosing.h</code>.</p>
  <p>I want to add a <code>Nested</code> class into the <code>Enclosing</code> and I want to be able to access from it a member of a <code>PImpl</code>.
  Is that even possible?</p>
  <p>For now I can not access <code>Enclosing::Nested</code> from main.
  I tried different configurations of where to declare and define <code>Nested</code>, but I didn't manage to do it so it works.</p>
  <p>The code:</p>
  <p>all_includes.h</p>
  <pre><code>#ifndef _TVG_ALL_INCLUDES_H_
  #define _TVG_ALL_INCLUDES_H_
  
  #include &lt;iostream&gt;
  using namespace std;
  
  struct Enclosing
  {
    int _e;
    Enclosing(int i);
   
    class PImpl;
    PImpl* _p;
   
    class Nested;
    Nested fun();
  };
  
  #endif
  </code></pre>
  <p>enclosing.cpp</p>
  <pre><code>#include &quot;enclosing.h&quot;
  #include &lt;iostream&gt;
  
  
  Enclosing::Enclosing(int i) : _e{i}, _p{new PImpl(2*i)}
  {
    cout &lt;&lt; &quot;Enclosing constr: &quot; &lt;&lt; _e &lt;&lt; endl;
  }
  
  
  Enclosing::Nested Enclosing::fun()
  {
    cout &lt;&lt; &quot;returns Enc::Nest &quot; &lt;&lt; endl;
    return Enclosing::Nested(0, nullptr);
  }
  </code></pre>
  <p>enclosing.h</p>
  <pre><code>#include &quot;all_includes.h&quot;
  
  
  class Enclosing::PImpl
  {
    int _ep;
  
    public:
    PImpl(int i) : _ep{i}
    {
      cout &lt;&lt; &quot;PImpl constr: &quot; &lt;&lt; _ep &lt;&lt; endl;
    }
  
    friend Enclosing::Nested;
  };
  
  
  class Enclosing::Nested
  {
    int _en;
    Enclosing* _ptr_enc;
  
    public:
    Nested(int i, Enclosing* p) : _en{i}, _ptr_enc{p}
    {
      cout &lt;&lt; &quot;Nested constr: &quot; &lt;&lt; _en &lt;&lt; endl;
    }
  
    void acces()  // this function is my goal - I want to access a PImpl member from Nested
    {
      cout &lt;&lt; &quot;PImpl private member: &quot; &lt;&lt; _ptr_enc-&gt;_p-&gt;_ep &lt;&lt; endl;
    }
  };
  </code></pre>
  <p>main.cpp</p>
  <pre><code>#include &quot;all_includes.h&quot;
  
  
  int main()
  {
    Enclosing* c = new Enclosing(17);
    //Enclosing::Nested n(22, c) ;   //comilator error: has initializer but incomplete type
  
    return 0;
  }
  </code></pre>
  <p>Makefile</p>
  <pre><code>CC=g++
  CCFLAGS=-std=c++14 -Wall
  
  main: main.o enclosing.o
          ${CC} ${CCFLAGS} -o main main.o enclosing.o
  
  main.o: main.cpp
          ${CC} ${CCFLAGS} -c main.cpp
  
  class1.o: enclosing.cpp
          ${CC} ${CCFLAGS} -c enclosing.cpp
  </code></pre>