title: I have included a template function declaration in a header file and the defintion of the function is written in the .cpp file
body: <p>I have declared a template function in .h file and defined in a .cpp file and calling the function from another .cpp file.</p>
  
  <p>i am getting the following error</p>
  
  <p>/tmp/ccJg5vwy.o: In function <code>main':
  main.cpp:(.text+0x8f): undefined reference to</code>short temp &lt; int, std::allocator &lt; int > >(std::__cxx11::list > const&amp;)' </p>
  
  <p>collect2: error: ld returned 1 exit status</p>
  
  <p>my files are:</p>
  
  <p>//main.cpp</p>
  
  <pre><code>#include"a.h"
  #include&lt;iostream&gt;
  #include&lt;list&gt;
  using namespace std;
  
  int main(){
      list&lt;int&gt; arr(5);
      for (int i=0;i&lt;5;i++)
          arr.push_back(i);
      fun1();
      fun2();
      temp(arr);
      return 0;
  }
  </code></pre>
  
  <p>//a.h</p>
  
  <pre><code>#include &lt;list&gt;
  using namespace std;
  void fun1();
  void fun2();
  template &lt;class L ,class A&gt;
      short temp(const list&lt;L,A&gt;&amp; val);
  </code></pre>
  
  <p>//fun2.cpp</p>
  
  <pre><code>#include"a.h"
  #include&lt;iostream&gt;
  using namespace std;
  void fun2(){
      cout&lt;&lt;"I am in function2\n";
  }
  </code></pre>
  
  <p>//a.cpp</p>
  
  <pre><code>#include "a.h"
  #include&lt;iostream&gt;
  using namespace std;
  void fun1()
  {
          cout&lt;&lt;"I am in function1\n";
  }
  template &lt;class L,class A&gt;
      short temp(const list&lt;L,A&gt;&amp; val){
          cout&lt;&lt;"inside the template function\n";
          typename list&lt;L,A&gt;::const_iterator iter=val.begin();
           while (iter != val.end())
                  {
                         cout&lt;&lt; *iter &lt;&lt; endl;
                         ++iter;
                  }
           cout&lt;&lt;"leaving the template function\n";
              return 0;
      }
  </code></pre>
  
  <p>and i am compiling the files in the following format</p>
  
  <p>g++ a.cpp fun2.cpp main.cpp</p>
  
  <p>I am not getting why there is a linking problem.</p>
  
  <p>I have also tried by changing a.cpp and a.h to the following format but still i am getting the error</p>
  
  <p>//a.cpp</p>
  
  <pre><code>#include "a.h"
  #include&lt;iostream&gt;
  using namespace std;
  void fun1()
  {
      cout&lt;&lt;"I am in function1\n";
  }
  template &lt;class L&gt;
      short temp(const list&lt;L&gt;&amp; val){
          cout&lt;&lt;"inside the template function\n";
          typename list&lt;L&gt;::const_iterator iter=val.begin();
          while (iter != val.end())
                  {
                         cout&lt;&lt; *iter &lt;&lt; endl;
                         ++iter;
                  }
           cout&lt;&lt;"leaving the template function\n";
              return 0;
      }   
  </code></pre>
  
  <p>//a.h</p>
  
  <pre><code>#include &lt;list&gt;
  using namespace std;
  void fun1();
  void fun2();
  template &lt;class L&gt;
  short temp(const list&lt;L&gt;&amp; val);
  </code></pre>
  
  <p>in this case error is</p>
  
  <p>/tmp/cce0eJj9.o: In function <code>main':
  main.cpp:(.text+0x8f): undefined reference to</code>short temp&lt; int >(std::__cxx11::list > const&amp;)'
  collect2: error: ld returned 1 exit status</p>