如果将类模板的声明和实现写在两个独立的文件中,在构建时会出现“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: 无法解析的外部符号的更多相关文章

  1. 使用模板时 error LNK2019: 无法解析的外部符号

    类模板是c++编译器指令 说明了如何生成类和成员函数 除非编译器实现了新的关键字export关键字 否则将模板成员函数放置在一个独立的实现文件中 将无法运行 因为模板不是函数 他们不能单独编译 模板必 ...

  2. error LNK2019 无法解析的外部符号------类模板和内敛函数

    今天用类模型实现一个单链表,开始是.h和.cpp将类模板的声明与实现分开写的,结果总是报错: 错误 error LNK2019: 无法解析的外部符号 ?$SingleList@H@@QAE@XZ),该 ...

  3. 在VS中使用类模板出现出现LNK2019: 无法解析的外部符号错误。

    在VS中使用类模板出现出现LNK2019: 无法解析的外部符号错误,应在一个.h文件中完成方法的声明与实现,不要将实现放在cpp文件里,VS貌似不支持类模板分离

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

  5. “error LNK2019: 无法解析的外部符号”之分析

    最近在用VS 2008开发,初学遇到不少问题,最头疼的问题之一就是:LNK2019. 百度一下讲的并不够全面,反正都没解决我的问题. error LNK2019问题在VC 6.0中是error LNK ...

  6. [转] “error LNK2019: 无法解析的外部符号”之分析

    HiLoveS原文“error LNK2019: 无法解析的外部符号”之分析 最近在用VS 2008开发,初学遇到不少问题,最头疼的问题之一就是:LNK2019. 百度一下讲的并不够全面,反正都没解决 ...

  7. error LNK2019: 无法解析的外部符号 "public:

    错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall test::test(void)" (??0test@@QAE@XZ),该符号在 ...

  8. 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Distance::Distance(int)" (??0Distance@@QAE@H@Z),该符号在函数 _main 中被引用

    错误: 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Distance::Distance(int)" (??0Distanc ...

  9. 摘:"error LNK2019: 无法解析的外部符号 该符号在函数 中被引用" 错误原因

    例如“error LNK2019: 无法解析的外部符号error LNK2001: 无法解析的外部符号“private: static struct _OVERLAPPED CUsbCom::g_Wr ...

随机推荐

  1. Java虚拟机及运行时数据区

    1.Java虚拟机的定义 Java虚拟机(Java Virtual Machine),简称JVM.当我们说起Java虚拟机时,可能指的是如下三种不同的东西: 抽象的虚拟机规范 规范的具体实现 一个运行 ...

  2. Quartz.NET总结(四)Quartz 远程调度

    前面篇已经介绍了Quartz.NET的配置,使用和Cron表达式表达式的写法.基本上后台的定时任务的定时执行已经完成,并能正确的按照执行计划,执行相关的job . 然后,如果任务需要更新,停止某个任务 ...

  3. 101 LINQ Samples

    https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

  4. 2016百度之星 初赛2A ABEF

    只做了1001 1002 1005 1006.剩下2题可能以后补? http://acm.hdu.edu.cn/search.php?field=problem&key=2016%22%B0% ...

  5. ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)

    ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...

  6. VBA学习思路

    打算花两三天学习VBA的基础,学习资料为<别怕,VBA其实很简单>,为了快速学习,先了解大致框架,后续再深入学习各种属性.方法和技巧. 1.VBA编程环境基本操作,手工操作,熟悉即可 2. ...

  7. Xamarin.Forms 简介

    An Introduction to Xamarin.Forms 来源:http://developer.xamarin.com/guides/cross-platform/xamarin-forms ...

  8. centos7 cannot find a valid baseurl for repo base (转载)

    centos7 cannot find a valid baseurl for repo base     今天在虚拟机下安装centosmini版本,安装后第一件事就是yum update 但是有错 ...

  9. Linux SVN 命令详解

    1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)   例如:svn checkout svn://192.168.1.1/pro/domain    ...

  10. linux Mint18 backspace怎么不能连续删除

    打开菜单--->首选项--->键盘,如下图所示: 打开启用重复按键即可,就可以随心所欲的连续删除,连续移动光标了