2016-02-09 39 views
0

我有以下代碼使用類定義讀取文本文件。我創建了TermGrade.h文件和TermGrade.cpp文件。但是我得到了一些錯誤:C++類讀取文本文件 - 獲取錯誤

在TermGrade.h文件中,我得到了一個警告,即它沒有爲定義的函數(Readdata,MidsemesterScore,FinalScore和LetterGrade)找到定義,即使我已經在其中定義了它們。 cpp文件。

但是,主要問題是在TermGrade.cpp文件中,我得到錯誤「錯誤:沒有重載函數的實例」getline「與參數列表匹配」,並且它也抱怨標識符「inLine」未定義,但它不會在下一個聲明中抱怨inline,但是該聲明表示 dataLines [lineNumber]未定義!我是否需要在.cpp文件中定義這些變量?任何幫助將不勝感激。

謝謝, 比爾。

// TermGrade.h file 

#ifndef TERMGRADE_H 
#define TERMGRADE_H 

#include <string> 
#include<iostream> 
#include<fstream> 
using namespace std; 

class TermGrade { 
public: 
    TermGrade(string fileName) {}; 
    string StudentID; 
    int assignments; 
    int exam1; 
    int exam2; 
    int final; 
    bool records = true; 

private: 
    string inLine; 
    string dataLines[100]; 
    int lineNumber = 0; 


    bool Readdata(istream& in);   // Read line of data from input file 
    double MidsemesterScore() const; // Calculates average 
    double FinalScore() const;   // Calculates average 
    char LetterGrade() const;   // Determines grade 


}; // end class Termgrade 

#endif 

// TermGrade.cpp file 

#include "TermGrade.h" // TermGrade class definition 
#include<iostream> 
#include<fstream> 

TermGrade::TermGrade(string fileName) { 
    ifstream infile; 
    infile.open(fileName); 

} 


bool Readdata(istream& infile) 
{ 
    if (!infile.eof) 
    { 
     getline(infile, inLine); 
     dataLines[lineNumber] = inLine; 
     return true; 
    } 
    return false; 
} 

double MidsemesterScore() 
{ 
    return 0.0; 
} 

double FinalScore() 
{ 
    return 0.0; 
} 

char LetterGrade() 
{ 
    return 'a'; 
} 
+0

你打算如何從'Readdata'函數中獲取數據?它不能訪問'this-> inLine',因爲'Readdata'不是'TermGrade'的成員。 –

回答

1

In the TermGrade.h file I get the warning that it cannot find a definition for any of the defined functions (Readdata, MidsemesterScore, FinalScore and LetterGrade) even though I have them defined in the .cpp file.

不,你不要讓他們在.cpp文件中定義。您沒有,例如TermGrade::Readdata方法的定義。該方法沒有在任何地方定義。你在那裏有一個叫做Readdata的函數,但是與TermGrade::Readdata方法無關。

關於幾個其他錯誤你得到的:

要調用一個名爲getline()功能,不過你還沒有定義的函數的任何位置,這就是爲什麼您獲得的編譯器錯誤。也許你打算從C++庫中引用std :: getline()函數,如果是的話,那麼這就是你應該指的。

另外,您傳遞的是一個名爲istream的類的引用,很遺憾,您並未在任何地方定義此類。如果您打算引用std::istream類,那麼您應該參考它,因此。

但是,std::istream類沒有名爲eof的成員。但它確實有eof()方法。

+0

和infile.eof()不會做你認爲它做的事情。你的意思是(infile) – pm100

+0

謝謝你的反饋山姆,它真的幫了大忙。 – user2278537