LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Passing ifstream& through derived class's member function (https://www.linuxquestions.org/questions/programming-9/passing-ifstream-and-through-derived-classs-member-function-845175/)

Eikcuhc 11-18-2010 05:55 PM

Passing ifstream& through derived class's member function
 
I am running into a problem when trying to compile. The program is supposed to read in text from a file that has the number of students, the student name, the class type, and the grades, then create an output file with the data. I have not gotten to the output part yet, as I run into compiling errors when using g++ to compile the three files with the command:

g++ main.cpp student.cpp student.h


student.cpp: In member function ‘void English::Input(std::ifstream&)’:
student.cpp:66: error: no match for ‘operator>>’ in ‘in1 >> ((English*)this)->English::attend’
student.cpp: In member function ‘void History::Input(std::ifstream&)’:
student.cpp:86: error: no match for ‘operator>>’ in ‘in1 >> ((History*)this)->History::term’
student.cpp: In member function ‘void Math::Input(std::ifstream&)’:
student.cpp:112: error: no match for ‘operator>>’ in ‘in1 >> ((Math*)this)->Math::quiz1’


STUDENT.H
Code:

#include <iostream>

enum coursecode {ENGLISH, HISTORY, MATH, NONE}; // None is used only for initializing class

class Student
{
public:
        virtual double Finalavg() = 0;
        Student();
        void Setstudent(const char* f, const char* l, const char* cc);
        coursecode Returncourse();

protected:
        char first[20];
        char last[20];
        coursecode course;
        double finalexam,
              finalavg;
};



class English : public Student
{
public:
        English();
        double Finalavg();
        void Input(std::ifstream& in1);

private:
        double attend,
              proj,
              mid;
};



class History : public Student
{
public:
        History();
        double Finalavg();
        void Input(std::ifstream& in1);

private:
        double term,
              mid;
};



class Math : public Student
{
public:
        Math();
        double Finalavg();
        void Input(std::ifstream& in1);

private:
        double quiz1,
              quiz2,
              quiz3,
              quiz4,
              quiz5,
              quizavg,
              test1,
              test2;
};


STUDENT.CPP
Code:

#include <iostream>
#include <cstring>
#include <iomanip>
#include "student.h"

using namespace std;

Student::Student()
{
        char word1[] = " ";
        char word2[] = " ";
        strncpy(first, word1, 20);
        strncpy(last, word2, 20);
        course = NONE;
        finalexam = 0;
        finalavg = 0;
}

void Student::Setstudent(const char* f, const char* l, const char* cc)
{
        char temp[7] = "NONE";
        strncpy(temp,cc,1);

        // Selects the cource code based on what is input
        // Since all the courses have a different starting letter,
        // we only need to compare the first letter
        switch(temp[0])
        {
                case 'E':
                case 'e':
                        course = ENGLISH;
                        break;
                case 'H':
                case 'h':
                        course = HISTORY;
                        break;
                case 'M':
                case 'm':
                        course = MATH;
                        break;
                default:
                        course = NONE;
                        break;
        }
       
        strncpy(first, f, 20);
        strncpy(last, l, 20);
}

coursecode Student::Returncourse()
{
        return course;
}

// ====================== ENGLISH ============================

English::English()
{
        attend = 0;
        proj = 0;
        mid = 0;
}

void English::Input(ifstream& in1)
{
        in1 >> attend >> proj >> mid >> finalexam;
}

double English::Finalavg()
{
        finalavg = (attend * .10) + (proj * .30)
                  + (mid * .30) + (finalexam * .30);
        return finalavg;
}

// ====================== HISTORY ============================

History::History()
{
        term = 0;
        mid = 0;
}

void History::Input(ifstream& in1)
{
        in1 >> term >> mid >> finalexam;
}

double History::Finalavg()
{
        finalavg = (term * .25) + (mid * .35)
                  + (finalexam * .40);
        return finalavg;
}

// ========================== MATH ===========================

Math::Math()
{
        quiz1 = 0;
        quiz2 = 0;
        quiz3 = 0;
        quiz4 = 0;
        quiz5 = 0;
        quizavg = 0;
        test1 = 0;
        test2 = 0;
}

void Math::Input(ifstream& in1)
{
        in1 >> quiz1 >> quiz2 >> quiz3 >> quiz4
            >> quiz5 >> test1 >> test2 >> finalexam;
}

double Math::Finalavg()
{
        quizavg = (quiz1 + quiz2 + quiz3 + quiz4 + quiz5) / 5;
        finalavg = (quizavg * .15) + (test1 * .25)     
                  + (test2 * .25) + (finalexam * .35);
        return finalavg;
}


MAIN.CPP
Code:

#include <iostream>
#include <fstream>
#include "student.h"

using namespace std;

int main()
{
        char input;            // Input file name
        char output;            // Output file name
        char temp[5] = "null";  // Used to pick up any white space
        ifstream in1;
        ofstream out1;
        int numstudents;        // Used to pick up amount of students in file
       

        cout << "Please enter the name of the input file.\n"
            << "Filename: ";
        cin >> input;
        cout << "Please enter the name of the output file.\n"
            << "Filename: ";
        cin >> output;
       
        // First we must input the first line, telling us how many students are
        // within the file
        in1 >> numstudents;   

        Student** list = new Student* [numstudents];
        in1.getline(temp,2);

        for (int i = 0; i < numstudents; i++)
        {
                char f[20],
                    l[20],
                    cc[20];

                in1.get(l, 20, ',');
                in1.getline(temp, 2, ' ');
                in1.getline(f, 20);
                in1.get(cc, 20, ' ');

                        if ((cc[0] == 'E') || (cc[0] == 'e'))
                        {
                                English* eng = new English;
                                eng->Setstudent(f, l, cc);
                                eng->Input(in1);
                                list[i] = eng;
                        }
                        else if ((cc[0] == 'H') || (cc[0] == 'h'))
                        {
                                History* hist = new History;
                                hist->Setstudent(f, l, cc);
                                hist->Input(in1);
                                list[i] = hist;
                        }
                        else if ((cc[0] == 'M') || (cc[0] == 'm'))
                        {
                                Math* math = new Math;
                                math->Setstudent(f, l, cc);
                                math->Input(in1);
                                list[i] = math;
                        }
        }

}


mac.tieu 11-20-2010 05:31 AM

You should add 'virtual void Input(std::ifstream&)=0' to Student class

neonsignal 11-20-2010 05:45 AM

You forgot to include fstream in the file student.cpp. Alternatively, you could just pass in the parameters as istream references, since the methods probably don't need to know it is a file.

Incidentally, when you compile your files, you do not need to add the header files to the compile line (since they have no code, and are included by the files that need them).
Code:

g++ main.cpp student.cpp


All times are GMT -5. The time now is 01:20 PM.