0
votes

I'm having trouble seeing why I'm getting an unresolved external symbol when creating an instance of a template class.

The line that causes the linker error is the call to new below:

Vbo<CustomVertex>*  m_pVbo;
...
m_pVbo = new Vbo<CustomVertex> ( 
  geometry.VertCount(), 
  geometry.Vertices(), 
  geometry.IndexCount(), 
  geometry.Indices() 
);

// nb: geometry.Vertices return type is CustomVertex**

The definition of the Vbo class is as follows:

template <typename T>
class Vbo : public glex
{
public:
  Vbo();
  Vbo( int nNumVerts, T** ppVertices, int nNumIndices, DWORD* pIndices );
  Vbo( const Vbo<T> & rhs );                        // copy
  Vbo<T> & operator=( const Vbo<T> & rhs );     // assignment
  ~Vbo();
...
}

And the implementation of the Vbo constructor :

template <typename T>
Vbo<T>::Vbo( int nNumVerts, T** ppVertices, int nNumIndices, DWORD* pIndices ) :    
  m_bInitialized    ( false ),  
  m_nVboId          ( 0 ),
  m_nVboIdIndex     ( 0 ),  
  m_nNumVertices    ( nNumVerts ),
  m_nNumIndices     ( nNumIndices ),
  m_ppVertices      ( ppVertices ),
  m_pIndices        ( pIndices )
{
    glex::Load();
    Initialize();
}

And finally, the complaint from the linker:

1>Actor.obj : error LNK2019: unresolved external symbol "public: __thiscall Vbo::Vbo(int,class CustomVertex * *,int,unsigned long *)" (??0?$Vbo@VCustomVertex@@@@QAE@HPAPAVCustomVertex@@HPAK@Z) referenced in function "private: bool __thiscall Actor::InitializeGeometry(class IGeometry &)" (?InitializeGeometry@Actor@@AAE_NAAVIGeometry@@@Z) 1>C:\cuprofen\Debug\Cuprofen.exe : fatal error LNK1120: 1 unresolved externals

Can someone spot my oversight?

1

1 Answers

1
votes

Where is the Vbo constructor defined? I am guessing it's in a .cpp or .cc file rather than the header file. Template function definitions need to be in the header (or you can use more exotic features like explicit template instantiation). This is because a template function is only understood as actual code when it is used, and I assume your use is not in the same translation unit as your definition.