title: Friend operators for template class
body: <p>I have a template class, to which I declared 2 <code>operator+</code> methods. Their declaration is:</p>
  
  <p>1) <code>const MyClass&lt;T&gt; operator+ (int num) const;</code></p>
  
  <p>and</p>
  
  <p>2) <code>friend const MyClass&lt;T&gt; operator+ &lt;&gt;(int  num, const MyClass&lt;T&gt;&amp; other);</code></p>
  
  <p>Following <a href="https://isocpp.org/wiki/faq/templates#template-friends" rel="nofollow" title="FAQ">this FAQ</a>, the .hpp file looks something like this:</p>
  
  <pre><code>//forward declaration of the class.
  template&lt;class T&gt;
  class MyClass;
  
  //forward declaration of the operator+
  template&lt;class T&gt;
  const MyClass&lt;T&gt; operator+ (int num, const MyClass&lt;T&gt;&amp; other);
  
  template&lt;class T&gt;
  class MyClass {
  public:
      ...
      const MyClass&lt;T&gt; operator+ (int num) const;
      friend const MyClass&lt;T&gt; operator+ &lt;&gt;(int num, const MyClass&lt;T&gt;&amp; other);
      ...
  };
  </code></pre>
  
  <p>(And later I have the definitions of those methods).</p>
  
  <p>Note that the 2 <code>operator+</code> methods are used in different situations:</p>
  
  <p>The first in</p>
  
  <pre><code>MyClass mc;
  mc+5;
  </code></pre>
  
  <p>And the second in</p>
  
  <pre><code>MyClass mc;
  5+mc;
  </code></pre>
  
  <p>But for some reason, when I compile it in g++ (version 4.8.2, if that matters), I get the error:</p>
  
  <pre><code>declaration of ‘operator+’ as non-function
     friend const MyClass&lt;T&gt; operator+ &lt;&gt;(int num, const MyClass&lt;T&gt;&amp; other);
  </code></pre>
  
  <p>Note that the error refers to the <code>friend</code> <code>operator+</code> method</p>
  
  <p>But, if I remove the declaration of the first <code>operator+</code> method (i.e. leaving only the <code>friend</code> one), then everything compiles just fine!</p>
  
  <p>What is going on?</p>