模板类 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?

时间: 2024-08-28 22:42:49

模板类 error LNK2019: 无法解析的外部符号的相关文章

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

最近在用VS 2008开发,初学遇到不少问题,最头疼的问题之一就是:LNK2019. 百度一下讲的并不够全面,反正都没解决我的问题. error LNK2019问题在VC 6.0中是error LNK2001: unresolved external symbol问题,可能错误号改了. 编译时出现类似这样的错误:Dlgcode.obj : error LNK2019: 无法解析的外部符号 _readRegmark,该符号在函数 [email protected] 中被引用.这种错误的本质是链接器

VS2013解决error LNK2019: 无法解析的外部符号 mfcs120ud.lib

最近因为要改一个MFC项目重写学习MFC,没想到只是用VS2013新建一个最简单的MFC项目运行就报类上百个的类似"Error LNK2019: 无法解析的外部符号...mfcs120ud.lib"的错误. 经过一番折腾,终于发现问题所在.打开C:\Users\Administrator\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props 发现默认库引用了$(WindowsSDK_LibraryPath

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

HiLoveS原文“error LNK2019: 无法解析的外部符号”之分析 最近在用VS 2008开发,初学遇到不少问题,最头疼的问题之一就是:LNK2019. 百度一下讲的并不够全面,反正都没解决我的问题. error LNK2019问题在VC 6.0中是error LNK2001: unresolved external symbol问题,可能错误号改了. 编译时出现类似这样的错误:Dlgcode.obj : error LNK2019: 无法解析的外部符号 _readRegmark,该符

error LNK2019: 无法解析的外部符号 &quot;class std::vector&lt;class std::basic_string&lt;char,struct std::char_traits&lt;cha

error LNK2019: 无法解析的外部符号 "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class

VS常见错误之一:error LNK2019: 无法解析的外部符号

由于经常使用VS的开发环境,所以经常遇到一些错误提示,其中error LNK2019是很常见的一个报错.今天将此错误的原因和常见解决办法根据自己的经验小小总结一下. 问题样式: 1>SingleView.obj : error LNK2019: 无法解析的外部符号 [email protected],该符号在函数 "protected: int __thiscall CSingleView::CreateViewGLContext(struct HDC__ *)" (?    

错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法

晚上花几分钟在windows下测了下pthread的用法,出现错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 经网上搜,反正都没解决,其中一个说引入#pragma comment(lib, "pthreadVC2.lib")后解决,但笔者遇到的不是这个原因,而是版本问题.可参考http://www.cnblogs.com/zhjh256/p/6364777.html解决.

error LNK2019: 无法解析的外部符号 [email&#160;protected],该符号在函数 ___tmainCR...(转)

一,问题描述 MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 [email protected],该符号在函数 ___tmainCRTStartup 中被引用  Debug\jk.exe : fatal error LNK1120: 1 个无法解析的外部命令 error LNK2001: unresolved external symbol [email protected] debug/main.exe:fatal error LNK 1

error LNK2019: 无法解析的外部符号

最近在尝试用UDT进行通信,写了两个简单的客户端和服务端,但是编译的时候出现了“error LNK2019: 无法解析的外部符号”错误,为了以后进行总结,还是把解决方法记录下来. 1>------ 已启动生成: 项目: udt_appserver, 配置: Debug Win32 ------1>生成启动时间为 2013/11/11 20:53:08.1>InitializeBuildStatus:1>  正在创建“Debug\udt_appserver.unsuccessfulb

error LNK2019: 无法解析的外部符号 [email&#160;protected],该符号在函数 _wmain 中被引用

1>sockett.obj : error LNK2019: 无法解析的外部符号 [email protected],该符号在函数 _wmain 中被引用 解决方法: 没有加入相应的链接库,winmm.lib.在头文件添加 #pragma comment(lib,"ws2_32.lib") ws2_32.lib文件,提供了对以下网络相关API的支持,若使用其中的API,则应该将ws2_32.lib加入工程(否则需要动态载入ws2_32.dll).有时无法直接对头文件进行修改.在引