title: Failed to link template class definitions with main program on VisualStudio 2010
body: <p>I wrote a template class which extends STL list for some use. I declare this class in a header file and define it in a cpp file. However, when I tried to test it in another cpp file, I found Visual Studio not able to link the functions to the cpp file.</p>
  
  <p>I know I can solve this problem by just including the cpp file, but I wonder if there is another way, a better way?</p>
  
  <p>In case you require further information of my code, I give my template class header file "KA_Buffer.h" and main program "main.cpp" below.</p>
  
  <p><strong>KA_Buffer.h:</strong></p>
  
  <pre><code>#pragma once
  
  #include &lt;list&gt;
  
  typedef int KA_Time;
  typedef std::list&lt;KA_Time&gt; KA_TimeList;
  
  /**
   * This is a length-limited buffer which would only save limited number of
   * elements.
   */
  template &lt;class T&gt;
  class KA_Buffer :
      public std::list&lt;T&gt;
  {
  public:
      KA_Buffer(int length) :
        m_MaxLength(length), m_Length(0) {};
      ~KA_Buffer(void) {};
  
      void add(T);
      void pop_front();
  
      iterator find(T);
  
  private:
      int m_Length;
      int m_MaxLength;
  };
  </code></pre>
  
  <p><strong>main.cpp:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  #include "KA_Buffer.h"
  
  int main()
  {
      KA_Buffer&lt;int&gt; buffer(5);
      for (int i = 0; i &lt; 10; i++)
      {
          buffer.add(i);
          for (KA_Buffer&lt;int&gt;::iterator iter = buffer.begin();
              iter != buffer.end(); iter++)
          {
              std::cout&lt;&lt;*iter&lt;&lt;" ";
          }
          std::cout&lt;&lt;std::endl;
      }
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>The error messages:</p>
  
  <blockquote>
    <p>1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall KA_Buffer::add(int)" (?add@?$KA_Buffer@H@@QAEXH@Z) referenced in function _main</p>
  </blockquote>