2015-01-21 68 views
-1

我需要一些帮助,我解决了链接错误,但我无法解释为什么为什么LNK2019未解决的外部错误出现在我的代码中?

这是我与我的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; 
    }; 

这是对应的program.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; 
} 

这是我在main.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> >)" ([email protected]@@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function _main 
error LNK2019: unresolved external symbol "public: void __thiscall InputData<int>::storeDataFromSourceFile(void)" ([email protected][email protected]@@QAEXXZ) referenced in function _main 
error LNK2019: unresolved external symbol "public: void __thiscall InputData<int>::putDataFromSourceFileIntoVector(class std::vector<int,class std::allocator<int> >)" ([email protected][email protected]@@[email protected][email protected]@[email protected]@@[email protected]@@Z) referenced in function _main 

这是通过从移动program.cpp函数定义的program.h解决,一切正常。

我在网上查了这个错误的原因,但由于某些原因,我对特定代码的解释仍然没有解决。

你能否给我一个具体的解释,为什么会发生这种情况?

PS:另外检查What is an undefined reference/unresolved external symbol error and how do I fix it?,但仍然找不到一个真正合乎逻辑的解释。

+1

您正在使用模板。您的解决方案*是正确的解决方案,那就是将实现移至标题。 http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – PaulMcKenzie 2015-01-21 10:36:12

+0

@PaulMcKenzie谢谢你,这是我正在寻找的答案。 – ciprianr 2015-01-21 10:38:32

回答

0

当你声明你的类时,你没有原型方法返回模板化变体。所以当它读取你的类声明并试图找到预期的函数时,它就失败了。

0

原因是模板对于标准编译和链接工具来说太高层次的构造。

C++设计中的一个目标是向后兼容性。 link.exe只能链接“机器代码”,但模板是“半生成代码”。所以你需要在头文件中实现所有的模板方法。编译器会找到它们并编译。

相关问题