调用模板ctor时未解析的外部符号

Unresolved external symbol when invoking a template ctor

本文关键字:外部 符号 ctor 调用      更新时间:2023-10-16

我很难理解为什么在创建模板类的实例时会得到未解析的外部符号。

导致链接器错误的行是对以下new的调用:

Vbo<CustomVertex>*  m_pVbo;
...
m_pVbo = new Vbo<CustomVertex> ( 
  geometry.VertCount(), 
  geometry.Vertices(), 
  geometry.IndexCount(), 
  geometry.Indices() 
);
// nb: geometry.Vertices return type is CustomVertex**

Vbo类的定义如下:

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();
...
}

以及Vbo构造函数的实现:

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();
}

最后,来自链接器的投诉:

1> Actor.obj:错误LNK2019:未解析的外部符号"public:__thiscall Vbo::Vbo(int,class CustomVertex**,int,unsigned long*)"(??0$Vbo@VCustomVertex@@@@QAE@HPAPAVCustomVertex@@HPAK@Z)在函数"private:boll__thiscall Actor::InitializeGeometry(class IGeometry&)"中引用(?InitializeGeometry@Actor@@AAE_NAAVI几何@@@Z)1> C:\cupofen\Debug\cuprofen.exe:致命错误LNK1120:1个未解析的外部

有人能发现我的疏忽吗?

Vbo构造函数在哪里定义?我猜它在.cpp或.cc文件中,而不是头文件中。模板函数定义需要在头中(或者可以使用更奇特的特性,如显式模板实例化)。这是因为模板函数只有在使用时才被理解为实际代码,并且我假设您的使用与您的定义不在同一个翻译单元中。