2015-01-21 34 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只能鏈接「機器代碼」,但模板是「半生成代碼」。所以你需要在頭文件中實現所有的模板方法。編譯器會找到它們並編譯。

相關問題