8

我有一个基于模板的类 [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();

private slots:
    void processConnections();
    void readFromSocket(int);

private:
 QTcpServer* rootServer;
 QSignalMapper* signalGate;
 Allotter<QTcpSocket> TcpAllotter;
};

我没有展示完整的定义,因为它并不重要。问题是当我编译时,所有文件都能正确编译。这些文件位于 VC++ 项目中。早些时候,当我没有使用基于模板的方法时Allotter,一切都可以正常编译和链接。但是现在,我收到了这个错误:

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)

令人惊讶的是,构造函数ActiveListener::ActiveListener()根本没有做任何引用Allotter<QTcpSocket>::Allotter()。然而,第二个参考确实存在。但我不明白为什么链接器无法解析这个外部符号。

错误出现之前的构建输出是:

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...

我不明白这是否相关,主要是因为以前所有这些都可以完美运行。只是我使用模板后出现问题。任何帮助将不胜感激。非常感谢。

4

3 回答 3

17

您不能将模板拆分为 .h 和 .cpp 文件 - 您需要将模板的完整代码放入 .h 文件中。

于 2009-12-05T17:45:01.857 回答
4

一般来说,将模板代码完全写在头文件中被认为是最佳实践。这有一个重要的技术原因:当您实例化模板时,C++ 编译器需要从该模板生成特定于您指定的模板参数的代码。如果您的模板代码完全放在标题中,这将自动为您完成。

完全可以按照您的方式编写模板代码,并将实现放在 cpp 文件中。但是,如果您这样做,则需要显式实例化您打算使用的模板实例。

在您的情况下,您需要将以下行添加到项目中的 .cpp 文件中:

template class Allotter<QTcpSocket>;
于 2009-12-06T07:53:17.313 回答
1

由于您不能将模板实现放在 .cpp 文件中,因此将 .inl 文件用于模板实现并将它们包含在模板头文件中被认为是一种很好的做法。

于 2009-12-05T17:57:38.200 回答