为什么我的代码会出现LNK2019未解决的外部错误
Why did the the LNK2019 unresolved external error appear for my code?
我需要一些帮助来解决我解决的链接器错误,但我无法解释为什么
这是我的代码,从我的program.h文件开始:
#pragma once
#include <vector>
#include <string>
#include <fstream>
template<class T> class InputData
{
public:
InputData(std::string);
void storeDataFromSourceFile();
void putDataFromSourceFileIntoVector(std::vector<T>);
private:
void inputLocationOfSourceFile();
void setSourceFileToLocation();
std::vector<T> inputVector;
std::string locationOfInputFile;
std::fstream sourceFile;
};
这是相应的程序.cpp代码:
#include <vector>
#include <string>
#include <fstream>
#include "InputData.h"
template<class T> InputData<T>::InputData(std::string source = "")
{
this->locationOfInputFile = source;
if (locationOfInputFile != "")
this->sourceFile.open(locationOfInputFile);
}
template<class T> void InputData<T>::inputLocationOfSourceFile()
{
std::cout << "Please enter location of source file";
std::cin >> this->locationOfInputFile;
}
template<class T> void InputData<T>::setSourceFileToLocation()
{
if (this->locationOfInputFile == "")
this->inputLocationOfSourceFile();
this->sourceFile.open(this->locationOfInputFile);
}
template<class T> void InputData<T>::storeDataFromSourceFile()
{
T inputElement;
if (this->locationOfInputFile == "")
this->setSourceFileToLocation();
while (this->sourceFile >> inputElement)
this->inputVector.push_back(inputElement);
}
template<class T> void InputData<T>::putDataFromSourceFileIntoVector(std::vector<T> destinationVector)
{
destinationVector = this->inputVector;
}
这是我的主要功能.cpp
#include <string>
#include <iostream>
#include <vector>
#include "InputData.h"
void printVector(std::vector<int> vectorToPrint)
{
for (std::vector<int>::const_iterator i = vectorToPrint.begin(); i != vectorToPrint.end(); ++i)
std::cout << *i << ' ';
}
int main()
{
InputData<int> sourceOfIntegers("Input.txt");
std::vector<int> destinationVector;
sourceOfIntegers.storeDataFromSourceFile();
sourceOfIntegers.putDataFromSourceFileIntoVector(destinationVector);
printVector(destinationVector);
}
当我使用 visual c++ 2013 构建程序时,这些是错误:
error LNK2019: unresolved external symbol "public: __thiscall InputData<int>::InputData<int>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0?$InputData@H@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
error LNK2019: unresolved external symbol "public: void __thiscall InputData<int>::storeDataFromSourceFile(void)" (?storeDataFromSourceFile@?$InputData@H@@QAEXXZ) referenced in function _main
error LNK2019: unresolved external symbol "public: void __thiscall InputData<int>::putDataFromSourceFileIntoVector(class std::vector<int,class std::allocator<int> >)" (?putDataFromSourceFileIntoVector@?$InputData@H@@QAEXV?$vector@HV?$allocator@H@std@@@std@@@Z) referenced in function _main
这是通过将函数定义从程序.cpp移动到program.h来解决的,一切正常。
我已经在网上检查了此错误的原因,但由于某些原因,对我的特定代码的解释仍然无法解释。
你能给我一个具体的解释,为什么会这样吗?
PS:还检查了什么是未定义的引用/未解析的外部符号错误以及如何解决它? 但仍然找不到真正合乎逻辑的解释。
原因是模板对于标准编译和链接工具来说太高级了。
C++设计的目标之一是向后兼容性。 link.exe
只能链接"机器码",但模板是"半生成代码"。因此,您需要在头文件中实现所有模板方法。编译器将找到它们并进行编译。
当您声明您的类时,您没有对返回模板化变体的方法进行原型设计。因此,当它读取您的类声明并尝试查找预期的函数时,它失败了。
相关文章:
- 节俭并发:未解决的外部问题
- 未解决的外部链接问题
- Visual Studio 2017 中的 QtMetaObject 未解决的外部错误
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 解释未解决的外部C++
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 函数路径追加上未解决的外部符号错误
- Lapack函数DGTSV通过C /Visual Studio提供未解决的外部符号
- 运算符过载上未解决的外部符号
- qfilesystemwatcher未解决的外部
- C 未解决的外部符号(公共静态INT)
- MIDL COM库中未解决的外部符号
- 未解决的外部符号sf ::带有SFML的时间
- C++编译错误LNK2019:未解决的外部符号错误
- VTK / ITK/ QT - 未解决的外部符号错误 (LNK2001)
- 未解决的外部包括AUX_KLIB内核库
- 单一实例对象的未解决外部符号链接错误
- 导出类静态常量的未解决外部符号链接错误(仅限某些项目)
- C++非常简单的未解决外部
- 如何纠正不一致的未解决外部错误