C ++模板,链接错误 [英] C++ template, linking error

查看:221
本文介绍了C ++模板,链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用我有一个模板类有一个问题。
我声明了一个新的类型名称数组,它是一个模板;

I have a problem in calling a template class I have. I declared a new type name Array, which is a template;

在.hpp文件中:

template <typename T>
class Array
{
public:
   Array();
};

在.cpp文件中:

template <typename T>
Array<T>::Array()
{
//Do something
}

在主体中:

Array<int> arr;

我收到链接错误:未解决的外部符号到ctor。

I get Linkage error: unresolved external symbol to the ctor.

任何想法?

推荐答案

模板功能(包括成员函数)这意味着如果你有一个模板类,它的实现必须完全在一个头。这是因为编译器需要访问整个模板定义(而不仅仅是签名),以便为模板的每个实例化生成代码。

Templates functions, including member functions, must be written entirely in headers. This means that if you have a template class, its implementation must be entirely in a header. This is because the compiler needs to have access to the entire template definition (not just the signature) in order to generate code for each instantiation of the template.

这篇关于C ++模板,链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆