views:

629

answers:

3

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.

+6  A: 

You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.

anon
Unless you use `export`, which only works in fairy tales.
Charles Salvia
@Charles:`export` works in real life (with the Comeau compiler).
Jerry Coffin
thanks a lot for that :DCould you just enlighten me as to why that happens? I'm assuming that whenever the compiler needs a specific class from a template i.e. MyClass<int> from MyClass<typename classType>, it needs to have the source code to generate the required class.Am I right?
Rohan Prabhu
Yes, you are right. All template definitions that are *parametrized* have to go into the .h file. Template definitions that is *not parametrized* (i.e. explicitly specialized templates) should be treated as "ordinary" code, meaning that classes are defined in .h files, while functions and obects are only *declared* in .h files and defined in .cpp files.
AndreyT
@Jerry the Comeau compiler ist unreal :-) Besides, `export` didn't make it into c++0x and probably never will.
drhirsch
@drhirsch: `export` is part of the current C++ standard, and it has not been removed or deprecated (yet) from C++0x. In addition, Comeau is not the only compiler that supports it; the Intel C++ Compiler for Linux also supports it.
James McNellis
FYI export has just been dropped from C++0x
anon
A: 

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.

rmn
A: 

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>;
Aaron Klotz