LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2010, 05:55 PM   #1
Eikcuhc
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
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;
                        }
        }

}
 
Old 11-20-2010, 05:31 AM   #2
mac.tieu
Member
 
Registered: Jan 2010
Location: Vietnam
Distribution: Arch
Posts: 65

Rep: Reputation: 22
You should add 'virtual void Input(std::ifstream&)=0' to Student class
 
Old 11-20-2010, 05:45 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
declare virtual function in derived class icecubeflower Programming 3 11-28-2009 09:04 PM
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
a class with a ifstream member Hano Programming 2 04-24-2002 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration