user2278537
user2278537

Reputation: 17

C++ class to read a text file - getting errors

I have the following code to read a text file using a class definition. I created the TermGrade.h file and the TermGrade.cpp file. But I get few errors:

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.

However, the main problem is that in the TermGrade.cpp file I get the error "Error:No instance of overloaded function "getline" matches the argument list" and it also complains that identifier "inLine" is undefined, but it does not complain about inLine in the next statement, but that statement says that both dataLines[lineNumber] are undefined! Do I need to define these variables in the .cpp file? Any help would be greatly appreciated.

Thank you, Bill.

// 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';
}

Upvotes: 0

Views: 63

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118425

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.

No, you don't have them defined in the .cpp file. You do not have, for example a definition of TermGrade::Readdata method. That method is not defined anywhere. You do have a function called Readdata in there, but that has nothing to do, whatsoever, with the TermGrade::Readdata method.

Regarding a few of the other errors you're getting:

You are calling a function called getline(), however you have not defined that function anywhere, and that's why you are getting that compiler error. Perhaps you meant to refer to the std::getline() function, from the C++ library, if so then that's what you should be referring to.

Also, you are passing a reference to a class called istream, unfortunately you did not define this class anywhere. If you meant to refer to the std::istream class, then you should've referenced it, as such.

However, the std::istream class does not have a member called eof. It does have an eof() method, though.

Upvotes: 1

Related Questions