title: C++: cannot define member function
body: <p>Could you please advise why I am getting the error in the code below?</p>
  
  <pre><code>error: cannot define member function ‘Test&lt;int&gt;::Printer::Print’ within ‘Test&lt;int&gt;’
  </code></pre>
  
  <p>I am using <code>gcc</code> version <code>8.1.1</code> and compile the code as <code>g++ -std=c++11</code>.</p>
  
  <p>Although, if I move the definition of function <code>Print</code> under the definition of <code>struct Printer</code> (i.e. making it inline implicitly), the compiler does not produce any error.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  template &lt;typename Type&gt;
  struct TestBase {
  
   struct Printer {
    template &lt;typename T&gt;
    void Print(const T&amp; t) {
      std::cout &lt;&lt; t &lt;&lt; std::endl;
    }
   };
  
  };
  
  template &lt;typename Type&gt;
  struct Test;
  
  template&lt;&gt;
  struct Test&lt;int&gt; : public TestBase&lt;int&gt; {
  
   struct Printer : public TestBase&lt;int&gt;::Printer {
    template &lt;typename T&gt;
    void Print(int i, const T&amp; t);
   };
  
   template &lt;typename T&gt;
   void Printer::Print(int i, const T&amp; t) {
    std::cout &lt;&lt; i &lt;&lt; t &lt;&lt; std::endl;
   }
  
  };
  
  int main() {
    Test&lt;int&gt; t;
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>Brian pointed out the exact reason why it is the case: "... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition..."</p>
  
  <p>Brian not only answered the main question that started this topic but also an additional question that I asked in the comment to the accepted answer of him.</p>