链接器错误“未解析的外部符号”:使用模板 [英] Linker error 'unresolved external symbol' : working with templates
问题描述
我有一个基于模板的类[Allotter.h& Allotter.cpp]:
template< typename allotType> class Allotter {
public:
Allotter();
quint32 getAllotment(allotType *);
bool removeAllotment(quint32,int auto_destruct = 0);
private:
QVector< QPair< quint32,allotType *>> indexReg;
int init_topIndex;
};
,它的用法显示为[ActiveListener.h& ActiveListener.cpp]:
class ActiveListener:public QObject {
Q_OBJECT
public:
ActiveListener();
私有插槽:
void processConnections();
void readFromSocket(int);
private:
QTcpServer * rootServer;
QSignalMapper * signalGate;
Allotter< QTcpSocket> TcpAllotter;
};
我没有显示完整的定义,因为它并不重要。问题是当我编译,所有文件编译正确。这些文件在一个VC ++项目中。早些时候,当我没有使用基于模板的方法 Allotter
时,一切都在编译和链接良好。但现在,我得到这个错误:
1> ActiveListener.obj:error LNK2019:未解析的外部符号public:__thiscall Allotter<在函数public:__thiscall ActiveListener :: ActiveListener(void)中引用的类QTcpSocket> :: Allotter< class QTcpSocket>(void)(?? 0?$ Allotter @ VQTcpSocket @@@@ QAE @ XZ) @@ QAE @ XZ)
$令人惊讶的是,构造函数
1> ActiveListener.obj:error LNK2019:未解析的外部符号public:unsigned int __thiscall Allotter< class QTcpSocket> :: getAllotment(class QTcpSocket *)(?getAllotment @?$ Allotter @函数private:void __thiscall ActiveListener :: processConnections(void)(?processConnections @ ActiveListener @@ AAEXXZ)中引用的VQTcpSocket @@@@ QAEIPAVQTcpSocket @@@ Z
ActiveListener :: ActiveListener()
根本没有提供任何引用。p $ p>
Allotter< QTcpSocket> :: Allotter()
。然而第二个参考文献存在。但我不明白为什么链接器不能解析这个外部符号。
错误出现之前的构建输出是:
1> Moc'ing ActiveListener.h ...
1>正在编译...
1> stdafx.cpp
1> Compiling ...
1> ActiveListener.cpp
1> Allotter.cpp
1> moc_ActiveListener.cpp
1> main.cpp
1>正在生成代码...
1>链接...
任何这是相关的,主要是因为所有这些以前工作完美。这只是,我使用模板后,导致一个问题。
任何帮助将不胜感激。非常感谢。解决方案您不能将模板分割为.h和.cpp文件 - 您需要将完整的代码.h文件中的模板。
I have a template based class [Allotter.h & Allotter.cpp]:
template <typename allotType> class Allotter { public: Allotter(); quint32 getAllotment(allotType*); bool removeAllotment(quint32, int auto_destruct = 0); private: QVector<QPair<quint32, allotType*>> indexReg; int init_topIndex; };
and it's usage is shown as [ActiveListener.h & ActiveListener.cpp]:
class ActiveListener: public QObject { Q_OBJECT public: ActiveListener(); private slots: void processConnections(); void readFromSocket(int); private: QTcpServer* rootServer; QSignalMapper* signalGate; Allotter<QTcpSocket> TcpAllotter; };
I am not showing the complete definitions, since it doesn't really matter. The problem is when I compile, all files compile properly. The files are in a VC++ project. Earlier when I did not use a template-based approach for
Allotter
, everything was compiling and linking fine. But now, I get this error:1>ActiveListener.obj : error LNK2019: unresolved external symbol "public: __thiscall Allotter<class QTcpSocket>::Allotter<class QTcpSocket>(void)" (??0?$Allotter@VQTcpSocket@@@@QAE@XZ) referenced in function "public: __thiscall ActiveListener::ActiveListener(void)" (??0ActiveListener@@QAE@XZ) 1>ActiveListener.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall Allotter<class QTcpSocket>::getAllotment(class QTcpSocket *)" (?getAllotment@?$Allotter@VQTcpSocket@@@@QAEIPAVQTcpSocket@@@Z) referenced in function "private: void __thiscall ActiveListener::processConnections(void)" (?processConnections@ActiveListener@@AAEXXZ)
The surprising thing is, that the constructor,
ActiveListener::ActiveListener()
does not make any reference at allAllotter<QTcpSocket>::Allotter()
. The second reference however does exist. But I don't understand why the linker isn't able to resolve this external symbol.The build output just before the errors appear is:
1>Moc'ing ActiveListener.h... 1>Compiling... 1>stdafx.cpp 1>Compiling... 1>ActiveListener.cpp 1>Allotter.cpp 1>moc_ActiveListener.cpp 1>main.cpp 1>Generating Code... 1>Linking...
I don't understand if any of this is relevant, mostly because all this used to work perfectly before. It's just that after I use templates a problem is caused. Any help will be appreciated. Thanks a lot.
解决方案You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.
这篇关于链接器错误“未解析的外部符号”:使用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!