使用模板成员函数时出现LNK2001错误
Getting LNK2001 error while using template member functions
所以我没有得到外部解析,我很困惑为什么它没有正确链接
内存.cpp:https://pastebin.com/x284CFTt
内存.hpp: https://pastebin.com/9xNcVY9q
主.cpp: https://pastebin.com/hQFTmn8E
输出窗口:
1>------ Build started: Project: NAME, Configuration: Release Win32 ------
1>main.cpp
1>memory.cpp
1>main.obj : error LNK2001: unresolved external symbol "public: bool __thiscall Memory::Write<int>(unsigned __int64,int)" (??$Write@H@Memory@@QAE_N_KH@Z)
1>main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Memory::Read<int>(unsigned __int64)" (??$Read@H@Memory@@QAEH_K@Z)
1>D:DesktopWorkbenchNAMEbinx86_ReleaseNAME.exe : fatal error LNK1120: 2 unresolved externals
1>Done building project "NAME.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
为什么它给我这些错误..我很困惑,我认为我正确地完成了类模板?
编译器需要模板类/函数声明和定义来创建特定类型的模板类/函数(在您的情况下int
(。
有以下可能的解决方案:
将模板声明和定义添加到头文件。
将每种类型的显式模板实例化添加到模板源文件。在您的情况下,将以下行添加到
memory.cpp
template int Memory::Read(DWORD64 Address);
template bool Memory::Write(DWORD64 Address, int dataBuffer);
- 包括模板源文件
memory.cpp
到main.cpp
。但是我认为这不是一个不错的解决方案。
相关文章:
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 将QT项目导入Visual Studio 2019会给我带来很多LNK2001错误
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 使用模板成员函数时出现LNK2001错误
- 错误 LNK2001:未解析的外部符号"private: static class Game Game::game_"
- LNK2001尝试将我的主函数分离为主.cpp时,我有一个奇怪的错误
- C++、错误LNK1120和LNK2001中未解析的外部符号
- 错误 LNK2001:VS2017 中未解析的外部符号___iob_func
- 使用外部变量会导致"unsigned char"错误LNK2001
- Dlib错误LNK2001未解析的外部符号
- VTK / ITK/ QT - 未解决的外部符号错误 (LNK2001)
- 互联网引起链接器错误lnk1120和lnk2001
- 使用boost::numpy时LNK2001错误
- LNK2001在X64环境中的编译代码上看到的错误.但是,该代码在X86环境中填充了罚款
- 我如何使用Bullet Physics SDK在VS2015中清除错误LNK2001
- LNK2001带有静态属性和方法的错误(QT,C )
- LNK2001:我的提升库(可能)构建错误
- 初始化 QList<MyStruct> 作为静态类成员给出LNK2001错误
- 命名空间内的变量LNK2001错误
- 使用 PCL 的广度第一迭代器时LNK2001错误