title: Overloading operator outside template struct
body: <p>I have the following code:</p>
  
  
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  using namespace std;
  
  template &lt;class F&gt;
  struct CMPLX {
  
      F Re, Im;
  
      struct _printnice {
          F Re, Im;
          string sep;
          _printnice(const F&amp; Re, const F&amp; Im, const string&amp; sep) : Re(Re), Im(Im), sep(sep) {}
      };
  
      CMPLX &lt;F&gt; (F Re, F Im) : Re(Re), Im(Im) {}
  
      _printnice PrintNice(const string&amp; sep="\t"){
          return _printnice(Re, Im, sep);
      }
  
  };
  
  template&lt;class F&gt;
  ostream&amp; operator &lt;&lt; (ostream&amp; os, const CMPLX&lt;F&gt; c){
      cout &lt;&lt; c.Re &lt;&lt; " + " &lt;&lt; c.Im &lt;&lt; "i";
  }
  
  template&lt;class F&gt;
  ostream&amp; operator &lt;&lt; (ostream&amp; os, const CMPLX&lt;F&gt; :: _printnice p){
      cout &lt;&lt; p.Re &lt;&lt; p.sep &lt;&lt; p.Im;
  }
  
  int main(){
      CMPLX&lt;float&gt; c(2.0,1.0);
      cout &lt;&lt; c &lt;&lt; endl;
      cout &lt;&lt; c.PrintNice() &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>I introduce a sub-struct <code>_printnice</code> in order to overload the operator <code>&lt;&lt;</code> and have differently-formatted output of my <code>CMPLX</code> class. However, this throws an error
  <strong>expected unqualified-id before ‘p’</strong> and I don't know how to solve this (my knowledge on templates is very limited).</p>
  
  <p>I try to change the 2nd definition of <code>&lt;&lt;</code> to the following which works, but I have to specify the type, which is frowned-upon:</p>
  
  <pre class="lang-c prettyprint-override"><code>ostream&amp; operator &lt;&lt; (ostream&amp; os, const CMPLX &lt;float&gt; :: _printnice p){
      cout &lt;&lt; p.Re &lt;&lt; p.sep &lt;&lt; p.Im;
  }
  </code></pre>