title: Overloading friend operator << for template class
body: <p>I'm trying to overload the operator &lt;&lt; as a friend to a template class Pair, but I keep getting a compiler warning saying</p>
  
  <pre><code>friend declaration std::ostream&amp; operator&lt;&lt;(ostream&amp; out, Pair&lt;T,U&gt;&amp; v) declares a non template function
  </code></pre>
  
  <p>for this code:</p>
  
  <pre><code>friend ostream&amp; operator&lt;&lt;(ostream&amp;, Pair&lt;T,U&gt;&amp;);
  </code></pre>
  
  <p>it gives a second warning as a recommendation saying</p>
  
  <pre><code>if this is not what you intended, make sure the function template has already been declared and add &lt;&gt; after the function name here
  </code></pre>
  
  <p>Here is the function definition</p>
  
  <pre><code>template &lt;class T, class U&gt;
  ostream&amp; operator&lt;&lt;(ostream&amp; out, Pair&lt;T,U&gt;&amp; v)
  {
      out &lt;&lt; v.val1 &lt;&lt; " " &lt;&lt; v.val2;
  }
  </code></pre>
  
  <p>and here is the whole class.</p>
  
  <pre><code>template &lt;class T, class U&gt;
  class Pair{
  public:
      Pair(T v1, U v2) : val1(v1), val2(v2){}
      ~Pair(){}
      Pair&amp; operator=(const Pair&amp;);
      friend ostream&amp; operator&lt;&lt;(ostream&amp;, Pair&lt;T,U&gt;&amp;);
  
  private:
      T val1;
      U val2;
  };
  </code></pre>
  
  <p>I wasn't sure what to draw from the recommendation warning, other than that maybe I have to put  somewhere in the friend declaration. Does anyone know the proper syntax for this? Thanks.</p>