c++ - 将带有模板的构造函数实现与头文件分离

标签 c++ templates

<分区>

Possible Duplicate:
Why should the implementation and the declaration of a template class be in the same header file?

我的头文件有

template <typename T>
class AA : public BB<T>
{
public:
    AA()
    { ... }

这工作正常。但我需要将构造函数实现与头文件分开。

所以在cpp中,我有

template <typename T>
AA<T>::AA()
{ ... }

当我编译它时,它编译了但我得到未解析的外部符号错误。我在这里缺少什么?

最佳答案

您可以在您的实现文件中显式实例化模板,使用:

template class AA<int>;

这将从模板生成一个定义,但只有当您知道您的类客户将使用什么类型时它才有用

关于c++ - 将带有模板的构造函数实现与头文件分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8674786/

相关文章:

c++ - 如何在 ubuntu 中使用 uchar_t 编译项目

c++ - 在 Qt 的槽函数中修改参数是否安全?

c++ - 作为非模板类​​成员的模板类变量

php - 模板化多语言网站的最有效方式

python - 如何避免在 webapp 模板中硬编码 url

c++ - 混合调试和发布库/二进制文件 - 不好的做法?

C++ - 如何初始化原子数组?

c++ - 如何将 std::chrono::system_clock::duration 转换为 struct timeval

templates - 在 eclipse 中编辑 dotcms 模板

c++ - C++ 中的模板类和函数重载