为什么我的代码会出现LNK2019未解决的外部错误

Why did the the LNK2019 unresolved external error appear for my code?

本文关键字:未解决 外部 错误 LNK2019 我的 代码 为什么      更新时间:2023-10-16

我需要一些帮助来解决我解决的链接器错误,但我无法解释为什么

这是我的代码,从我的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只能链接"机器码",但模板是"半生成代码"。因此,您需要在头文件中实现所有模板方法。编译器将找到它们并进行编译。

当您声明您的类时,您没有对返回模板化变体的方法进行原型设计。因此,当它读取您的类声明并尝试查找预期的函数时,它失败了。