title: defining friend function within templated class
body: <p>I use three different files to define a templated class. The class declaration is in a <code>.h</code> file, the implementation in a <code>.cpp</code> file and explicit instantiations are included in a <code>.inc</code> file.
  I'm trying to define a friend function that it's able to access private data members of a templated class. 
  As in the case of templated classes, the function will be defined, implemented, and instantiated in 3 separate files.
  When I try the call the function, I get the following error messages:</p>
  
  <p><code>myclass.h:error: ‘doSomething’ is neither function nor member function; cannot be declared friend</code></p>
  
  <p><code>myclass.h:error: expected ‘;’ before ‘&lt;’ token</code></p>
  
  <p><code>mymethod.h: error: ‘friend’ used outside of class</code></p>
  
  <p>Does anybody has any suggestion on how to solve this issue? I tried to simplify the code below.</p>
  
  <h3>myclass.h</h3>
  
  <pre><code>  template&lt;class T&gt;
    class MyClass{
  
        friend T doSomething&lt;T&gt;( MyClass&lt;T&gt; *, T, int);
        friend  MyClass&lt;T&gt; * doSomethingElse&lt;T&gt;( const MyClass&lt;T&gt; *, const MyClass&lt;T&gt; *);
        public:
                 ...
        private:
           T *m_pMyMatrix;
    };
  </code></pre>
  
  <h3>mymethod.h</h3>
  
  <pre><code>#include &lt;myclass.h&gt;
  template &lt;class T&gt; friend T doSomething( MyClass&lt;T&gt; *, T, int);
  template &lt;class T&gt; MyClass&lt;T&gt;* doSomethingElse(const MyClass&lt;T&gt;*, const MyClass&lt;T&gt;*);
  </code></pre>
  
  <h3>mymethod.cpp</h3>
  
  <pre><code>#include &lt;mymethod.h&gt;
  template &lt;class T&gt;
  T doSomething( MyClass&lt;T&gt; * pData, T val, int index){
     // the actual code does sth. more complex than the code below.
     pData-&gt;m_pMyMatrix[index]+=val;
     return pData-&gt;m_pMyMatrix[index];
  }
  
  template &lt;class T&gt;
  MyClass&lt;T&gt;* doSomethingElse(const MyClass&lt;T&gt; * pData1, const MyClass&lt;T&gt; * pData2){
     ...
     T res1 = doSomething(pData1, val1, index1);
     ...
  }
  #include "mymethod-impl.inc"
  </code></pre>
  
  <h3>mymethod-impl.inc</h3>
  
  <pre><code>template float doSomething( MyClass&lt;float&gt; *, float, int);
  template double doSomething( MyClass&lt;double&gt; *, double, int);
  
  template MyClass&lt;float&gt;* doSomethingElse(const MyClass&lt;float&gt;*, const MyClass&lt;float&gt;*);
  template MyClass&lt;double&gt;* doSomethingElse(const MyClass&lt;double&gt;*, const MyClass&lt;double&gt; *);
  </code></pre>