Yet another overloading operators thread, your help is severely needed

This is a discussion on Yet another overloading operators thread, your help is severely needed within the C++ Programming forums, part of the General Programming Boards category; We were given our original project back and asked to overload all the operators, even if we did use them ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Yet another overloading operators thread, your help is severely needed

    We were given our original project back and asked to overload all the operators, even if we did use them we had to overload them. The teacher said we would have to rewrite some of our code to make the overloading work. I am clueless as to even how to begin to implement overloading.

    Here is my original code: It reads in a file that holds the scores of students, then outputs the data into another file with each student's average appended to the end and the overall average for each test(quiz1 quiz2 etc....) underneath their respective column.

    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class Grades
    {
    public:
    	Grades();
    	Grades(int quiz1, int quiz2, int midterm, int final);
    	void Input(ifstream& Fin);
    	void Output(ofstream& Fout);
    	int GetQuiz1();
    	int GetQuiz2();
    	int GetMidterm();
    	int GetFinal();
    	void SetQuiz1(int NewQuiz1);
    	void SetQuiz2(int NewQuiz2);
    	void SetMidterm(int NewMidterm);
    	void SetFinal(int NewFinal);
    
    private:
    	int mQuiz1;
    	int mQuiz2;
    	int mMidterm;
    	int mFinal;
    };
    
    int main()
    {
    	Grades Student;
    	Grades Sum; //will be used to get average of all quizes
    	double Avg;
    	double Quiz1TotalAverage;
    	double Quiz2TotalAverage;
    	double FinalTotalAverage;
    	double MidtermTotalAverage;
    
        ifstream Fin;
    	ofstream Fout;
    
    	Fin.open("input.txt");
    		if(Fin.fail())
    		{
    			cout << "Input file opening failed.\n";
    			exit(1);
    		}
    
    	Fout.open("output.txt");
    		if(Fout.fail())
    		{
    			cout << "Output file opening failed.\n";
    			exit(1);
    		}
    	
         Fout << " Quiz1 " << " Quiz2 " << " Midterm " << "      Final " << " Average " << endl;
    
              
    
    	while(!(Fin.eof()))
    	{
    		Student.Input(Fin);
    		Student.Output(Fout);
    		Avg = 20.0 * (Student.GetQuiz1() + Student.GetQuiz2())/20 + (20.0 * Student.GetMidterm())/100 + (60.0 * Student.GetFinal())/100;
    		//calculates the averages for the whole class for quiz1, quiz2, midterm and final exams. 
    		Sum.SetQuiz1(Sum.GetQuiz1() + Student.GetQuiz1()); 
    		Sum.SetQuiz2(Sum.GetQuiz2() + Student.GetQuiz2());
    		Sum.SetFinal(Sum.GetFinal() + Student.GetFinal());
    		Sum.SetMidterm(Sum.GetMidterm() + Student.GetMidterm());
    		Fout << setw(10) << Avg << endl;
    	}
        //the total average for each quiz, midterm and final is set equal to a variable
    	//whose value will be output to the output file
    	Quiz1TotalAverage = Sum.GetQuiz1()/15;
    	Quiz2TotalAverage = Sum.GetQuiz2()/15;
    	FinalTotalAverage = Sum.GetFinal()/15;
    	MidtermTotalAverage = Sum.GetMidterm()/15;
    
    	Fout << "___" << setw(10) << "___" << setw(10) << "___" << setw(10) << "___" << endl;
    	Fout << Quiz1TotalAverage << setw(10) << Quiz2TotalAverage << setw(10) << FinalTotalAverage << setw(10) <<MidtermTotalAverage;
    
    	Fin.close();
    	Fout.close();
    
    	return 0;
    
    }
    
    Grades::Grades() : mQuiz1(0), mQuiz2(0), mMidterm(0), mFinal(0)
    {
    }
    
    Grades::Grades(int mQuiz1, int mQuiz2, int mMidterm, int mFinal)
    {
    }
    
    void Grades::Input(ifstream& Fin)
    {
    	Fin >> mQuiz1 >> mQuiz2 >> mMidterm >> mFinal;
    }
    
    void Grades::Output(ofstream& Fout)
    {
        Fout << mQuiz1 << setw(10)<< mQuiz2 << setw(10)<< mMidterm << setw(10)<< mFinal;
    }
    
    int Grades::GetQuiz1()
    {
    	return mQuiz1;
    }
    int Grades::GetQuiz2()
    {
    	return mQuiz2;
    }
    
    int Grades::GetMidterm()
    {
    	return mMidterm;
    }
    
    int Grades::GetFinal()
    {
    	return mFinal;
    }
    
    void Grades::SetQuiz1(int NewQuiz1)
    {
    	mQuiz1 = NewQuiz1;
    }
    
    void Grades::SetQuiz2(int NewQuiz2)
    {
    	mQuiz2 = NewQuiz2;
    }
    
    void Grades::SetMidterm(int NewMidterm)
    {
    	mMidterm = NewMidterm;
    }
    
    void Grades::SetFinal(int NewFinal)
    {
    	mFinal = NewFinal;
    }


    Here are some of my attempts in implementing overloading:


    Code:
    class Grades
    {
    public:
    	Grades();
    	Grades(int quiz1, int quiz2, int midterm, int final);
    	friend Grades operator +(const Grade& Test1, const Grade& Test2);
    	void Input(ifstream& Fin);
    	void Output(ofstream& Fout);
    	int GetQuiz1();
    	int GetQuiz2();
    	int GetMidterm();
    	int GetFinal();
    	void SetQuiz1(int NewQuiz1);
    	void SetQuiz2(int NewQuiz2);
    	void SetMidterm(int NewMidterm);
    	void SetFinal(int NewFinal);
    
    private:
    	int mQuiz1;
    	int mQuiz2;
    	int mMidterm;
    	int mFinal;
    };
    
    int main()
    {
    	
    	Grades Sum; //will be used to get average of all quizes
    	Grades Student;
    	Grades Study(GetQuiz1(), GetQuiz2(), GetMidterm(), GetFinal())
    	Grades total
    Code:
    Grades operator +(const Grades& Test1, const Grades& Test2)
    {
    	Grades temp;
    
    	temp = Test1.mQuiz1 + Test1.mQuiz2;
    
    	return temp;
    }


    As stated before, I don't know what direction to go in and i have to overload
    the + - * / == << and >> operators.

    So where do i start? What needs to be replaced and what just needs to be augmented. Thank you in advance

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    From what I can gather, all my overloading will deal with what's in my while loop

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Well, it's really easy to overload the stream extraction/insertion operators (>> and <<). That can clean up the look of the loop a bit and you can get rid of the Input and Output functions:
    Code:
    class Grades
    {
    public:
        ...
        friend ostream& operator<<(ostream&,const Grades&);
        friend istream& operator>>(istream&,Grades&);
        ...
    };
    
    ostream& operator<<(ostream& os, const Grades& rhs)
    {
        return os << rhs.mQuiz1 << setw(10) << rhs.mQuiz2
                  << setw(10) << rhs.mMidterm << setw(10) << rhs.mFinal;
    }
    
    istream& operator>>(istream& is, Grades& rhs)
    {
        return is >> rhs.mQuiz1 >> rhs.mQuiz2 >> rhs.mMidterm >> rhs.mFinal;
    }
    Then the while loop can look like this:
    Code:
    while(Fin >> Student)
    {
        Fout << Student;
        ...
    }
    Now all you need is something (you'll have to make an attempt and show us what you've got) to handle the addition so you can have a Sum object and each iteration through the loop, you add (+) the current Student to the Sum object so you have a running total of all the different grades. Your addition operator should give you something similar to:
    Code:
    Grades Student;
    Grades Sum;
    int counter = 0;
    while( Fin >> Student )
    {
        Fout << Student;
        Sum = Sum + Student; // Keep a running total of all the grades
        ++counter;
    }
    // Now output the averages
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Unbelievable, this helps me more then you know. I can't thank you enough.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    i made the suggested alterations and overloaded my operators, but now the compiler is giving me errors having to deal with my functions' handling of private member variables.

    Here is the code and erros


    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class Grades
    {
    public:
    	Grades();
    	Grades(int quiz1, int quiz2, int midterm, int final);
    	friend ostream& operator <<(ostream& Outs, const Grades& Rhs);
        friend istream& operator >>(istream& Ins, Grades& Rhs);
        
    	friend Grades operator +(const Grades& Lhs, const Grades& Rhs);
        friend Grades operator *(const Grades& Lhs, const Grades& Rhs);
    	friend Grades operator /(const Grades& Lhs, const Grades& Rhs);
    	friend Grades operator -(const Grades& Lhs, const Grades& Rhs);
    	friend bool operator ==(const Grades& Lhs, const Grades& Rhs);
    	
    	int GetQuiz1();
    	int GetQuiz2();
    	int GetMidterm();
    	int GetFinal();
    	void SetQuiz1(int NewQuiz1);
    	void SetQuiz2(int NewQuiz2);
    	void SetMidterm(int NewMidterm);
    	void SetFinal(int NewFinal);
    
    private:
    	int mQuiz1;
    	int mQuiz2;
    	int mMidterm;
    	int mFinal;
    };
    
    int main()
    {
    	Grades Student;
    	Grades Sum; //will be used to get average of all quizes
    	double Avg;
    	double Quiz1TotalAverage;
    	double Quiz2TotalAverage;
    	double FinalTotalAverage;
    	double MidtermTotalAverage;
    	int counter = 0;
    
        ifstream Fin;
    	ofstream Fout;
    
    	Fin.open("input.txt");
    		if(Fin.fail())
    		{
    			cout << "Input file opening failed.\n";
    			exit(1);
    		}
    
    	Fout.open("output.txt");
    		if(Fout.fail())
    		{
    			cout << "Output file opening failed.\n";
    			exit(1);
    		}
    	
         Fout << " Quiz1 " << " Quiz2 " << " Midterm " << "      Final " << " Average " << endl;
    
                     /*******************************************************************************
                     This loop executes until the entire file has been read and formula has been applied.
                     *******************************************************************************/
    
    	while(!(Fin.eof()))
    {
    	Fin >> Student; 
        Fout << Student;
    	Avg = 20.0 * (Student.GetQuiz1() + Student.GetQuiz2())/20 + (20.0 * Student.GetMidterm())/100 + (60.0 * Student.GetFinal())/100;
        Sum = Sum + Student; // Keeps a running total of all the grades
    	Fout << setw(10) << Avg << endl;
        counter++;
    }
    
    	 
               //the total average for each quiz, midterm and final is set equal to a variable
    	//whose value will be output to the output file
    	Quiz1TotalAverage = Sum/counter;
    	Quiz2TotalAverage = Sumcounter;
    	FinalTotalAverage = Sum/counter;
    	MidtermTotalAverage = Sum/counter;
    
    	Fout << "___" << setw(10) << "___" << setw(10) << "___" << setw(10) << "___" << endl;
    	Fout << Quiz1TotalAverage << setw(10) << Quiz2TotalAverage << setw(10) << FinalTotalAverage << setw(10) <<MidtermTotalAverage;
    
    	Fin.close();
    	Fout.close();
    
    	return 0;
    
    }
    
    Grades::Grades() : mQuiz1(0), mQuiz2(0), mMidterm(0), mFinal(0)
    {
    }
    
    Grades::Grades(int mQuiz1, int mQuiz2, int mMidterm, int mFinal)
    {
    }
    
    istream& operator >>(istream& Ins, Grades& Rhs)
    {
        return Ins >> Rhs.mQuiz1 >> Rhs.mQuiz2 >> Rhs.mMidterm >> Rhs.mFinal;
    }
    
    ostream& operator <<(ostream& Outs, const Grades& Rhs)
    {
        return Outs << Rhs.mQuiz1 << setw(10) << Rhs.mQuiz2
    		     << setw(10) << Rhs.mMidterm << setw(10) << Rhs.mFinal;
    }
    
    
    Grades operator +(const Grades& Lhs, const Grades Rhs) 
    {
    	Grades temp;
    
    	temp.mQuiz1 = Lhs.mQuiz1 + Rhs.mQuiz1;
    	temp.mQuiz2 = Lhs.mQuiz2 + Rhs.mQuiz2;
    	temp.mMidterm = Lhs.mMidterm + Rhs.mMidterm;
    	temp.mFinal = Lhs.mFinal + Rhs.mFinal;
    
    	return temp;
    }
    
    Grades operator -(const Grades& Lhs, const Grades Rhs) 
    {
    	Grades temp;
    
    	temp.mQuiz1 = Lhs.mQuiz1 - Rhs.mQuiz1;
    	temp.mQuiz2 = Lhs.mQuiz2 - Rhs.mQuiz2;
    	temp.mMidterm = Lhs.mMidterm - Rhs.mMidterm;
    	temp.mFinal = Lhs.mFinal - Rhs.mFinal;
    
    	return temp;
    }
    
    Grades operator /(const Grades& Lhs, const Grades& Rhs) 
    {
    	Grades temp;
    
    	temp.mQuiz1 = Lhs.mQuiz1 / Rhs.mQuiz1;
    	temp.mQuiz2 = Lhs.mQuiz2 / Rhs.mQuiz2;
    	temp.mMidterm = Lhs.mMidterm / Rhs.mMidterm;
    	temp.mFinal = Lhs.mFinal / Rhs.mFinal;
    
    	return temp;
    }
    
    /*bool operator ==(const Grades& Lhs, const Grades& Rhs)
    {
    	Grades temp;
    
    	temp.mQuiz1 = Lhs.mQuiz1 == Rhs.mQuiz1;
    	temp.mQuiz2 = Lhs.mQuiz2 == Rhs.mQuiz2;
    	temp.mMidterm = Lhs.mMidterm == Rhs.mMidterm;
    	temp.mFinal = Lhs.mFinal == Rhs.mFinal;
    
    	return temp;
    }*/
    
    Grades operator *(const Grades& Lhs, const Grades& Rhs) 
    {
    	Grades temp;
    
    	temp.mQuiz1 = Lhs.mQuiz1 * Rhs.mQuiz1;
    	temp.mQuiz2 = Lhs.mQuiz2 * Rhs.mQuiz2;
    	temp.mMidterm = Lhs.mMidterm * Rhs.mMidterm;
    	temp.mFinal = Lhs.mFinal * Rhs.mFinal;
    
    	return temp;
    }
    
    
    int Grades::GetQuiz1()
    {
    	return mQuiz1;
    }
    int Grades::GetQuiz2()
    {
    	return mQuiz2;
    }
    
    int Grades::GetMidterm()
    {
    	return mMidterm;
    }
    
    int Grades::GetFinal()
    {
    	return mFinal;
    }
    
    void Grades::SetQuiz1(int NewQuiz1)
    {
    	mQuiz1 = NewQuiz1;
    }
    
    void Grades::SetQuiz2(int NewQuiz2)
    {
    	mQuiz2 = NewQuiz2;
    }
    
    void Grades::SetMidterm(int NewMidterm)
    {
    	mMidterm = NewMidterm;
    }
    
    void Grades::SetFinal(int NewFinal)
    {
    	mFinal = NewFinal;
    }


    Code:
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(139) : error C2248: 'Grades::mQuiz1' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(35) : see declaration of 'Grades::mQuiz1'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(139) : error C2248: 'Grades::mQuiz1' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(35) : see declaration of 'Grades::mQuiz1'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(139) : error C2248: 'Grades::mQuiz1' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(35) : see declaration of 'Grades::mQuiz1'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(140) : error C2248: 'Grades::mQuiz2' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(36) : see declaration of 'Grades::mQuiz2'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(140) : error C2248: 'Grades::mQuiz2' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(36) : see declaration of 'Grades::mQuiz2'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(140) : error C2248: 'Grades::mQuiz2' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(36) : see declaration of 'Grades::mQuiz2'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(141) : error C2248: 'Grades::mMidterm' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(37) : see declaration of 'Grades::mMidterm'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(141) : error C2248: 'Grades::mMidterm' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(37) : see declaration of 'Grades::mMidterm'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(141) : error C2248: 'Grades::mMidterm' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(37) : see declaration of 'Grades::mMidterm'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(142) : error C2248: 'Grades::mFinal' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(38) : see declaration of 'Grades::mFinal'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(142) : error C2248: 'Grades::mFinal' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(38) : see declaration of 'Grades::mFinal'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(142) : error C2248: 'Grades::mFinal' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(38) : see declaration of 'Grades::mFinal'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(151) : error C2248: 'Grades::mQuiz1' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(35) : see declaration of 'Grades::mQuiz1'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(151) : error C2248: 'Grades::mQuiz1' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(35) : see declaration of 'Grades::mQuiz1'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(151) : error C2248: 'Grades::mQuiz1' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(35) : see declaration of 'Grades::mQuiz1'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(152) : error C2248: 'Grades::mQuiz2' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(36) : see declaration of 'Grades::mQuiz2'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(152) : error C2248: 'Grades::mQuiz2' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(36) : see declaration of 'Grades::mQuiz2'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(152) : error C2248: 'Grades::mQuiz2' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(36) : see declaration of 'Grades::mQuiz2'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(153) : error C2248: 'Grades::mMidterm' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(37) : see declaration of 'Grades::mMidterm'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(153) : error C2248: 'Grades::mMidterm' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(37) : see declaration of 'Grades::mMidterm'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(153) : error C2248: 'Grades::mMidterm' : cannot access private member declared in class 'Grades'
            c:\documents and settings8\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(37) : see declaration of 'Grades::mMidterm'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(154) : error C2248: 'Grades::mFinal' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(38) : see declaration of 'Grades::mFinal'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(154) : error C2248: 'Grades::mFinal' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(38) : see declaration of 'Grades::mFinal'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'
    c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(154) : error C2248: 'Grades::mFinal' : cannot access private member declared in class 'Grades'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(38) : see declaration of 'Grades::mFinal'
            c:\documents and settings\my documents\visual studio 2005\projects\anotest\anotest\tazor.cpp(12) : see declaration of 'Grades'

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by selftest View Post
    i made the suggested alterations and overloaded my operators, but now the compiler is giving me errors having to deal with my functions' handling of private member variables.

    Here is the code and erros


    [CODE]Grades operator -(const Grades& Lhs, const Grades Rhs)
    {
    Grades temp;

    temp.mQuiz1 = Lhs.mQuiz1 - Rhs.mQuiz1;
    temp.mQuiz2 = Lhs.mQuiz2 - Rhs.mQuiz2;
    temp.mMidterm = Lhs.mMidterm - Rhs.mMidterm;
    temp.mFinal = Lhs.mFinal - Rhs.mFinal;

    return temp;
    }
    Your code is trying to access private member variables of the objects.
    I propose you make access functions like Set/Get, which might be the usual approach. Otherwise you can make those member variables public (not recommended), or you can declare class Grades as a friend:
    Code:
    class Grades
    {
    	friend class class Grades;
    	...
    };
    It may be weird for a class to declare itself as a friend, but hey, it works. Since Grades is trying to work with itself, I believe it's safe to use friend here.
    But you should always be careful in declaring friends. You can read up about them a little if you want to use them.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    It may be weird for a class to declare itself as a friend, but hey, it works.
    I disagree: a class declaring itself as a friend is meaningless.

    From what I see, the solution is to declare operator- as a friend. A possibly better solution is to define operator-= as a member, then implement operator- in terms of operator-=.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Declaring the class as a friend of itself doesn't make sense. A class is already a friend of itself.

    The problem is that your friend declarations for two of the operators don't match the definition of those operators. There is one small typo in the definitions of operator+ and operator-. Just fix the typo and you'll be good (except there are probably a couple other compiler errors to fix).

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Consider a more refined approach to writing code which involves you compiling and testing a lot more often than you seem to be doing at present.

    Getting dozens of error messages then dumping the whole mess on a forum for someone else to fix isn't a good strategy.

    Especially since most of your errors seem to be of the 'cut-n-paste' variety, in that had you spotted the problem early enough, then none of the later errors would ever have appeared.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    ^you are right my friend, in the future I will work harder to better analyze my code. I'd like to thank you all for the help once again.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    class Grades
    {
        ...  
        friend Grades operator +(const Grades& Lhs, const Grades& Rhs);
        friend Grades operator -(const Grades& Lhs, const Grades& Rhs);
        ...
    };
    
    ...
    
    Grades operator +(const Grades& Lhs, const Grades Rhs) 
    {
        ...
    }
    
    Grades operator -(const Grades& Lhs, const Grades Rhs) 
    {
        ...
    }
    Those don't match, there's a subtle difference. This is the typo mentioned by Daved.


    Also...
    Code:
    int counter = 0;
    
    Quiz1TotalAverage = Sum/counter;
    Quiz2TotalAverage = Sumcounter;
    FinalTotalAverage = Sum/counter;
    MidtermTotalAverage = Sum/counter;
    You don't have an operator defined that divides a Grades object by an int. And Sumcounter is obviously a typo.


    Last...
    Code:
    while(!(Fin.eof()))
    {
        Fin >> Student; 
        Fout << Student;
        ...
    }
    Don't do that. eof gets set after a failed attempt to read. After the last line of grades is read, eof will still be false which means you'll try to go through the loop one last time... which is not what you want. Prefer this:
    Code:
    while( Fin >> Student )
    {
        Fout << Student;
        ...
    }
    Last edited by hk_mp5kpdw; 10-26-2007 at 09:31 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Elysia View Post
    Your code is trying to access private member variables of the objects.
    I propose you make access functions like Set/Get, which might be the usual approach.
    There is no need to do that.
    The problem comes from the definition not matching the befriended declaration (It's missing the ampersand.) Best to always start with a copy and paste of the declaration when writing the function definition.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by iMalc View Post
    There is no need to do that.
    The problem comes from the definition not matching the befriended declaration (It's missing the ampersand.) Best to always start with a copy and paste of the declaration when writing the function definition.
    Really, so you can access private members on const reference objects?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Really, so you can access private members on const reference objects?
    Yes, if you are a friend function that does not modify those private member variables. selftest defined operator- to be such a function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Yes, but I wasn't referring to friend functions (or classes).
    In the above operator, whether or not the parameter were references and const or not, it is unable to do anything unless it's a friend.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text RPG - Overloading Operators
    By legit in forum Game Programming
    Replies: 9
    Last Post: 11-25-2008, 01:47 PM
  2. overloading operators
    By thracian in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2008, 08:23 PM
  3. overloading operators for vector addition
    By cunnus88 in forum C++ Programming
    Replies: 10
    Last Post: 10-13-2006, 03:59 AM
  4. overloading postfix/prefix operators for a class
    By jpipitone in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2003, 10:06 PM
  5. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21