2014-11-02 164 views
-3

我只是想看看我是否能讀取文本文件和顯示,但我有此錯誤:C++錯誤LNK 2019

2 error LNK2019: unresolved external symbol "public: void __thiscall WeatherReport::displayReport(void)" ([email protected]@@QAEXXZ) referenced in function _main

誰能給我解釋一下是什麼原因造成這一點,爲什麼發生這種情況,以及如何解決這個問題?

#include<fstream> 
    #include<iomanip> 
    #include<stdio.h> 
    #include<cmath> 
    #include<iostream> 

    using namespace std; 

    class WeatherReport 
    { 
     WeatherReport friend monthEnd(WeatherReport, WeatherReport); 
     private: 
      int dayofMonth; 
      int highTemp; 
      int lowTemp; 
      double amoutRain; 
      double amoutSnow; 

     public: 
      WeatherReport(int Day = 0); 
      void setValues(int, int, int, double, double); 
      void getValues(); 
      void displayReport(); 
    } 
    void WeatherReport::setValues(int dom, int ht, int lt, double ar, double as) 
    { 
     dayofMonth = dom; 
     highTemp = ht; 
     lowTemp = lt; 
     amoutRain = ar; 
     amoutSnow = as; 
    } 

    int main() 
    { 
     const int DAYS = 30; 
     WeatherReport day[DAYS]; 
     WeatherReport summary; 
     int i = 0; 

     ifstream inFile; 
     inFile.open("WeatherTest.txt"); 
     if (!inFile) 
      cout << "File not opended!" << endl; 
     else 
     { 
      int dom, ht, lt; 
      double ar, as; 
      while (inFile >> dom >> ht >> lt >> ar >> as) 
      { 
       day[i].setValues(dom, ht, lt, ar, as); 
       i++; 
      } 
     inFile.close(); 

     for (int i = 0; i < DAYS; i++) 
     { 
      day[i].displayReport(); 
      //read one line of data from the file 
      //pass the data to setValues to initialize the object 
     } 
     system("PAUSE"); 
     return 0; 
    } 
+0

是的,它是重複的,隊長Obv ......等等。說真的,這個問題已經被***問過很多次了。 ***查看我的個人資料'關於我'***。 – cybermonkey 2014-11-02 20:28:49

+1

-1可怕的凹痕 – GingerPlusPlus 2014-11-02 20:31:28

+0

好的,謝謝。一些我沒有看到的。感謝您的鏈接 – Meene 2014-11-02 20:39:27

回答

0

displayReport沒有函數體,因此不具有external symbol引用它,因此錯誤。 添加函數體displayReport,這個問題就會迎刃而解:

void WeatherReport::displayReport() 
{ 
    //Place your code here. 
} 

下面的代碼可用於重現此錯誤:

[頭文件 - test.h]:

#include "StdAfx.h" 

void someOtherFunction(); 
void someFunction(string thisVar); 

[代碼文件級TEST.CPP]:

#include "StdAfx.h" 
#include "test.h" 

void someOtherFunction() 
{ 
    printf("Hello World!"); 
} 

[函數體someFunction(string thisVar) is missing!]

0

錯誤本身就說明了

LNK2019: unresolved external symbol "public: void __thiscall WeatherReport::displayReport(void) 

它不能找到定義WeatherReport::displayReport()。我在你的代碼中看到它的聲明,但是在任何地方都沒有定義。要麼你沒有寫出定義,要麼你提供了它,並且沒有鏈接它所在的文件。我在猜測前者。

0

好像displayReport()沒有一個正文 - 它只是聲明的,但沒有定義。添加以下內容:

void WeatherReport::displayReport() 
{ 
    //your code 
}