模板类 error LNK2019: 无法解析的外部符号
如果将类模板的声明和实现写在两个独立的文件中,在构建时会出现“error LNK2019: 无法解析的外部符号 ”的错误。
解决方法有:
- 第一种方法,就是把类模板中成员函数的声明和定义都放在类的定义中(.h文件),不要分开就行。
- 第二种方法,在主文件(main文件)中既包含类模板的声明文件(接口文件)(.h文件),同时也包含类模板的实现文件(.cpp文件)就行了。
- 第三种方法,在类的定义中(.h文件)的最后包含类模板的实现文件(.cpp文件)。
原因在于模板类和模板函数在使用的时候才会被实例化。当模板被使用时,编译器需要函数所有的实现代码,来用合适的类型(模板参数)去构建正确的函数。但是如果将函数实现在单独的源文件中,这些文件是不可见的,因而会出错。
下面是摘自 StackOverflow的回答:
Anyway, the reason your code is failing is that, when instantiating a template, the compiler creates a new class with the given template argument. For example:
template<typename T>
struct Foo
{
T bar;
void doSomething(T param) {/* do stuff using T */ }
}; // somewhere in a .cpp
Foo<int> f;
When reading this line, the compiler will create a new class (let's call it FooInt), which is equivalent to the following:
struct FooInt
{
int bar;
void doSomething(int param) {/* do stuff using int */ }
}
Consequently, the compiler needs to have access to the implementation of the methods, to instantiate them with the template argument (in this case int). If these implementations were not in the header, they wouldn't be accessible, and therefore the compiler wouldn't be able to instantiate the template.
参考:
CSDN:模板类 error LNK2019: 无法解析的外部符号
StackOverflow:Why can templates only be implemented in the header file?
模板类 error LNK2019: 无法解析的外部符号的更多相关文章
- 使用模板时 error LNK2019: 无法解析的外部符号
类模板是c++编译器指令 说明了如何生成类和成员函数 除非编译器实现了新的关键字export关键字 否则将模板成员函数放置在一个独立的实现文件中 将无法运行 因为模板不是函数 他们不能单独编译 模板必 ...
- error LNK2019 无法解析的外部符号------类模板和内敛函数
今天用类模型实现一个单链表,开始是.h和.cpp将类模板的声明与实现分开写的,结果总是报错: 错误 error LNK2019: 无法解析的外部符号 ?$SingleList@H@@QAE@XZ),该 ...
- 在VS中使用类模板出现出现LNK2019: 无法解析的外部符号错误。
在VS中使用类模板出现出现LNK2019: 无法解析的外部符号错误,应在一个.h文件中完成方法的声明与实现,不要将实现放在cpp文件里,VS貌似不支持类模板分离
- error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> >
1,VS2013: 错误 1 error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_trai ...
- “error LNK2019: 无法解析的外部符号”之分析
最近在用VS 2008开发,初学遇到不少问题,最头疼的问题之一就是:LNK2019. 百度一下讲的并不够全面,反正都没解决我的问题. error LNK2019问题在VC 6.0中是error LNK ...
- [转] “error LNK2019: 无法解析的外部符号”之分析
HiLoveS原文“error LNK2019: 无法解析的外部符号”之分析 最近在用VS 2008开发,初学遇到不少问题,最头疼的问题之一就是:LNK2019. 百度一下讲的并不够全面,反正都没解决 ...
- error LNK2019: 无法解析的外部符号 "public:
错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall test::test(void)" (??0test@@QAE@XZ),该符号在 ...
- 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Distance::Distance(int)" (??0Distance@@QAE@H@Z),该符号在函数 _main 中被引用
错误: 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Distance::Distance(int)" (??0Distanc ...
- 摘:"error LNK2019: 无法解析的外部符号 该符号在函数 中被引用" 错误原因
例如“error LNK2019: 无法解析的外部符号error LNK2001: 无法解析的外部符号“private: static struct _OVERLAPPED CUsbCom::g_Wr ...
随机推荐
- Java学习笔记15
do-while循环时while循环的变体语法如下:do{ // 循环体 语句(组);}while(循环继续条件); 如果循环中的语句至少需要执行一次,那么建议使用do-while循环. for循环 ...
- Nginx配置文件
https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structur ...
- Python之路【第二十三篇】爬虫
difference between urllib and urllib2 自己翻译的装逼必备 What is the difference between urllib and urllib2 mo ...
- Oracle修改字段类型方法总结
有一个表名为tb,字段段名为name,数据类型nchar(20). 1.假设字段数据为空,则不管改为什么字段类型,可以直接执行:alter table tb modify (name nvarchar ...
- C# WinForm 技巧:控件截图
Point screenPoint = 控件.PointToScreen(new Point()); Rectangle rect = new Rectangle(screenPoint, chart ...
- 为何img、input等内联元素可以设置宽、高
元素是文档结构的基础,在CSS中,每个元素生成了一个包含了元素内容的框(box,也译为“盒子”).但是不同的元素显示的方式会有所不同,例如<div>和<span>就不同,而&l ...
- jq绑定事件的4种方式
jQuery提供了多种绑定事件的方式,每种方式各有其特点,明白了它们之间的异同点,有助于我们在写代码的时候进行正确的选择,从而写出优雅而容易维护的代码.下面我们来看下jQuery中绑定事件的方式都有哪 ...
- sqlservcer行列互转
普通行列转换 行转列 假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/---- ...
- 让用VS2013编写的程序在XP中顺利运行
vs2013编译好的exe文件拖到虚拟机xp中无法运行,提示错误,从网上搜索.找答案. 参考:http://blog.csdn.net/asanscape/article/details/387526 ...
- No.11
每天干点杂活,充实自己,获得一种短暂的安全感......