调用模板构造函数时无法解析的外部符号
我无法理解为什么在创建模板类的实例时出现未解析的外部符号。
导致链接器错误的行是下面对 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: bool __thiscall Actor::InitializeGeometry(class IGeometry &)”中引用 (?InitializeGeometry@Actor@@AAE_NAAVIGeometry@ @@Z) 1>C:\cuprofen\Debug\Cuprofen.exe : 致命错误 LNK1120: 1 未解析的外部
有人能发现我的疏忽吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Vbo
构造函数在哪里定义?我猜测它位于 .cpp 或 .cc 文件中,而不是头文件中。模板函数定义需要位于标头中(或者您可以使用更奇特的功能,例如显式模板实例化)。这是因为模板函数仅在使用时才被理解为实际代码,并且我假设您的使用与您的定义不在同一翻译单元中。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.