title: Defining pure virtual functions from .h and .cpp files yields linker error?
body: <p>I was trying to put together a 'framework' for all my lab works, but then I ran into a frustrating linker error dealing with the implementation of pure virtual function..</p>
  
  <p>When I define the pure virtual function from a .cpp file ( like returntype classname::function() {.....} ) I get a linker error, telling me that the definition of the pure virtual function is not provided... </p>
  
  <p>However, when i simply put the definition to the header file, it works well.. I know i sound confusing... but the code below will surely help you see what is happening.. </p>
  
  <p>Can anyone please help me understand why is this happening ?</p>
  
  <p>The project contains 4 files, ( 2 headers, and 2 cpp files)</p>
  
  <p>1 > FrameWork.h :</p>
  
  <pre><code> #ifndef _FRAMEWORK
   #define _FRAMEWORK
  
   #include&lt;iostream&gt;
  
   class labTest
  
   {
     public :
        virtual void execute() = 0;
   };
  #endif
  </code></pre>
  
  <p>======================================</p>
  
  <p>2 >Stack_Array.h :</p>
  
  <pre><code>#include "FrameWork.h"
  #include&lt;iostream&gt;
  using namespace std;
  
  template &lt;class T&gt;
  class Stack_Array : public labTest
    {
  
         public: 
           virtual void execute();
    };
  </code></pre>
  
  <p>======================================</p>
  
  <p>3 > Stack_Array.cpp : </p>
  
  <pre><code>#include "Stack_Array.h"
  template&lt;class T&gt;
  
   virtual void Stack_Array&lt;T&gt;::execute(void) // I beleive i am defining the pure virtual function here, but my compiler ll not agree.
    {
        std::cout &lt;&lt; "Test";
    }
  </code></pre>
  
  <p>======================================</p>
  
  <p>4 > Main_Run.cpp :</p>
  
  <pre><code>#include&lt;istream&gt;
  #include"FrameWork.h"
  #include"Stack_Array.h"
  #include&lt;vector&gt;
  using namespace std;
  
  void main()
    {
     vector&lt;labTest*&gt; list(5);
     vector&lt;labTest*&gt;::iterator it;
     it = list.begin();
     Stack_Array&lt;int&gt;* sa = new Stack_Array&lt;int&gt;();
  
     list.insert(it,sa);
     list[0]-&gt;execute();
     getchar();
  
    }
  </code></pre>
  
  <p>=========================================</p>
  
  <p>Build Output : </p>
  
  <pre><code>1&gt;------ Rebuild All started: Project: Lab FrameWork, Configuration: Debug Win32 ------
  1&gt;Build started 11/20/2012 6:16:48 PM.
  1&gt;InitializeBuildStatus:
  1&gt;  Touching "Debug\Lab FrameWork.unsuccessfulbuild".
  1&gt;ClCompile:
  1&gt;  Stack_Array.cpp
  1&gt;  Main_Run.cpp
  1&gt;  Generating Code...
  1&gt;Main_Run.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Stack_Array&lt;int&gt;::execute(void)" (?execute@?$Stack_Array@H@@UAEXXZ)
  1&gt;C:\Users\BSP-4\Documents\Visual Studio 2010\Projects\SFML\Lab FrameWork\Debug\Lab FrameWork.exe : fatal error LNK1120: 1 unresolved externals
  1&gt;
  1&gt;Build FAILED.
  1&gt;
  1&gt;Time Elapsed 00:00:01.64
  
  ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped =======================
  </code></pre>
  
  <p>It works if I make my Stack_Array.h :</p>
  
  <pre><code>#include "FrameWork.h"
  #include&lt;iostream&gt;
  using namespace std;
  
  template &lt;class T&gt;
  class Stack_Array : public labTest
  {
  
      public: 
      void execute() // give the definition here instead of Stack_Array.cpp and it will work !
         {
             cout &lt;&lt; "Test Success !!";
         }
  };
  </code></pre>
  
  <p>I am sure its some silly thing.. Did I overlooked something ?.. but am still in need of help.... </p>
  
  <p>Thanks in advance...</p>