Linker error 'unresolved external symbol' : working with templates
c++, linker-errors, qt4, templates, visual-c++
Solution
You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.
Problem
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 all `Allotter<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.