title: Template Factory Pattern in C++
body: <p>I am trying to make a factory that will have the type passed in, rather then having it hard coded for types. However, when I attempt to add the type to the factory inside of the types .cpp file, I will get a linker error. For example, he is a linker error I am currently getting.</p>
  
  <p>1>RandomClass.obj : error LNK2019: unresolved external symbol "public: short __thiscall TemplatedFactory::AddType(char const *)" (??$AddType@VRandomClass@@@TemplatedFactory@@QAEFPBD@Z) referenced in function "void _<em>cdecl `dynamic initializer for 'private: static short RandomClass::ID''(void)" (??</em>_E?ID@RandomClass@@0FA@@YAXXZ)</p>
  
  <p>I tried to make the test case as small as possible, though it does cross five files, they are very small</p>
  
  <p>BaseClass.h : <a href="http://codepad.org/MhZMw7t0" rel="nofollow">http://codepad.org/MhZMw7t0</a></p>
  
  <pre><code>#pragma once
  class BaseClass{ };
  </code></pre>
  
  <p>RandomClass.h : <a href="http://codepad.org/xoObzP8G" rel="nofollow">http://codepad.org/xoObzP8G</a></p>
  
  <pre><code>#pragma once
  #include "BaseClass.h"
  class RandomClass : public BaseClass
  {
  private:
      static short ID;
  
  public:
      RandomClass();
      virtual ~RandomClass();
  };
  </code></pre>
  
  <p>TemplatedFactory.h : <a href="http://codepad.org/qkcTBw24" rel="nofollow">http://codepad.org/qkcTBw24</a></p>
  
  <pre><code>#pragma once
  #include &lt;map&gt;
  using std::map;
  #include "BaseClass.h"
  
  template&lt;typename Type&gt; BaseClass* createType() { return new Type; }
  
  class TemplatedFactory
  {
  private:
      typedef BaseClass* (*ComponentFactoryFuncPtr)();
      typedef map&lt;const char*, ComponentFactoryFuncPtr&gt; map_type;
  
      map_type m_Map;
  
  public:
      static TemplatedFactory &amp;GetInstance();
  
      template&lt;typename Type&gt;
      short AddType(const char* componentName);
  };
  </code></pre>
  
  <p>RandomClass.cpp : <a href="http://codepad.org/ALoF3Ysb" rel="nofollow">http://codepad.org/ALoF3Ysb</a></p>
  
  <pre><code>#include "RandomClass.h"
  #include "TemplatedFactory.h"
  
  short RandomClass::ID = TemplatedFactory::GetInstance().AddType&lt;RandomClass&gt;("RandomClass");
  
  RandomClass::RandomClass() { }
  
  RandomClass::~RandomClass() { }
  </code></pre>
  
  <p>TemplatedFactory.cpp : <a href="http://codepad.org/iqgNqa6H" rel="nofollow">http://codepad.org/iqgNqa6H</a></p>
  
  <pre><code>#include "TemplatedFactory.h"
  
  TemplatedFactory &amp;TemplatedFactory::GetInstance()
  {
      static TemplatedFactory instance;
      return instance;
  }
  
  template&lt;typename Type&gt;
  short TemplatedFactory::AddType(const char* componentName)
  {
      ComponentFactoryFuncPtr function = &amp;createType&lt;Type&gt;;
      m_Map.insert(std::make_pair(componentName, function));
  
      return 0;
  }
  </code></pre>
  
  <p>I can remove the the linker error if I move</p>
  
  <pre><code>short RandomClass::ID = TemplatedFactory::GetInstance().AddType&lt;RandomClass&gt;("RandomClass");
  </code></pre>
  
  <p>from RandomClass.cpp to TemplatedFactory.cpp, however, I would like to have the declaration in RandomClass.cpp. Does anyone know of a way to fix this or perhaps a better design (without the use of external libraries).</p>