简体   繁体   中英

Unresolved external symbol when invoking a template ctor

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM