模板类 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 ...
随机推荐
- p/invoke碎片--对数组的封送处理
因为数组是引用类型,所以数组的处理根据数组元素的类型是否为“可直接传递到非托管代码”的类型而分为两种情况.主要目标是看内存是怎么变化的,是复制还是锁定. 数组中的元素是"可直接传递到非托管代 ...
- PHP读取EXCEL时间
在使用php读取excel表格中的时间时得到一串数字而不是时间:40359.58333333334 excel 中的时间值是自1900年以来的天数,注意是格林威治时间php 中的时间值是自1970年以 ...
- Angular 之坑??
1. 场景:bootstrap的popup画面中有一个select元素, <select > <option> 1 </option> <option ng- ...
- setTimeout 和 setInterval 的区别
setTimeout (表达式,延时时间)setInterval(表达式,交互时间)延时时间/交互时间是以豪秒为单位的(1000ms=1s) setTimeout 在执行时,是在载入后延迟指定时间 ...
- nginx+Memcached 缓存设计
单页面缓存方案 单静态页缓存 解决问题场景 常见的缓存设计利用System.Web.Cache 保存在内存内,效率高,可以减轻数据库访问的压力.但是Web除了获取数据之外,还有呈现页面渲染,生成HTM ...
- 输出 Office 报表
以 word 为例: 将 word 存为 Word2003 XML,其中苹果等部分即是 xml 如下: 服务器端通常输出 HTML,HTML 是文本,XML也是文本,可以简单的套用服务器端网页的思路. ...
- windows安装rabbitmq
官网下载windows安装版本:http://www.rabbitmq.com/install-windows.html ,安装文件rabbitmq-server-3.6.5.exe 前提:安装erl ...
- C++程序设计基础
01 1 预编译常用的有,宏定义和包含库.2 库:是实用工具的集和,由程序员编写,可以完成一些特定的功能.3 <> 系统库 ""用户自定义库.4 宏定义:定义符号常量, ...
- 无中间变量交换swap(a,b)
#include <stdio.h> /* 加减法 整型.浮点型(损失精度) */ void swap1(int *a,int *b) { *a=*a+*b; *b=*a-*b; *a=* ...
- Windows10下的JDK环境配置。
首先附上JDK资源: 下载地址:Java SE Development Kit 8u112 PS:32位系统下载X86,64位系统下载X64 JDK安装: 此处省略安装步骤..... PS:记住JDK ...