Linker error 'unresolved external symbol' : working with templates
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>Acti开发者_如何学运维veListener.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.
You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.
Generally speaking it is considered best practice to write your template code entirely inside header files. There is an important technical reason for this: when you instantiate a template, the C++ compiler needs to generate code from that template that is specific to the template parameters that you have specified. If your template code is placed entirely in your headers, this is done for you automatically.
It is definitely possible to write template code the way that you have, with the implementation placed in cpp files. If you do this, however, you are required to explicitly instantiate the template instance that you intend to use.
In your case, you need to add the following line to a .cpp file in your project:
template class Allotter<QTcpSocket>;
Since you can't place template implementation in .cpp files, it is considered a good practice to use .inl files for the template implementation and include them from the template headers.
精彩评论