Problem with being friends with another class

Started by
13 comments, last by Thunder_Hawk 18 years, 7 months ago
Ok, I'm stuck. I've done this before, but now it seems to not work anymore. Yeah, it's homework assignment, but I've already did everything, just getting this error about ostream unable to access rational class private stuff... This is my weakness... Can you explain what I'm doing wrong? I'm stumped...

class Rational
{
...
public:
friend ostream& operator << (ostream&, Rational&);
};

..

ostream& operator << (ostream& bout, Rational& Rat)
{
bout << Rat.Numerator << "/" << Rat.Denominator;

return bout;
}
The error I'm getting is this:

C:\Games\Brent's Games\Homework\HW1\Rational.cpp(158) : error C2248: 'Numerator' : cannot access private member declared in class 'Rational'
        C:\Games\Brent's Games\Homework\HW1\Rational.cpp(7) : see declaration of 'Numerator'
C:\Games\Brent's Games\Homework\HW1\Rational.cpp(172) : error C2248: 'Denominator' : cannot access private member declared in class 'Rational'
        C:\Games\Brent's Games\Homework\HW1\Rational.cpp(8) : see declaration of 'Denominator'
Much obliged. [Edited by - Zeraan on September 5, 2005 10:55:54 PM]
Advertisement
I think you should remove the operator << from the class definition itself.

Toolmaker

The problem is that I try to declare that Rational class is friends with ostream/istream class, which allows me to add operator overloading for << and >>. But it acts like if I didn't do that, which is the cause of the error...

Why is this occuring?

#include <iostream>using namespace std;class Rational{	int Numerator;	int Denominator;	void Simplify();public:	Rational() { Numerator = 0; Denominator = 1; }	Rational(int N, int D) { Numerator = N; Denominator = D; } //Constructor	void operator = (Rational);	Rational operator + (Rational);  //Basic arthrimtic functions	Rational operator - (Rational);	Rational operator * (Rational);	Rational operator / (Rational);		friend ostream& operator << (ostream&, Rational&);	friend istream& operator >> (istream&, Rational&);};int main(){	Rational R1(5,3);	Rational R2(6,2);	Rational Temp;	R1 = R2;	for(;;)	{		int Choice = 0;		cout	<< endl << "Welcome to Zeraan's Rationalization Center!"				<< endl << "What would you like to do?  Input between 1 to 6:"				<< endl << "1. Input Rationals"				<< endl << "2. Add Rationals"				<< endl << "3. Subtract Rationals"				<< endl << "4. Multiply Rationals"				<< endl << "5. Divide Rationals"				<< endl << "6. Quit Rationalizing!"				<< endl << "Input your decision: ";		cin >> Choice;		switch(Choice)		{		case	1:	break;		case	2:	Temp = R1 + R2;						break;		case	3:	Temp = R1 - R2;					break;		case	4:	Temp = R1 * R2;					break;		case	5:	Temp = R1 / R2;					break;		case	6:	goto EndOfProgram; //I'm just being funny here :)					//Actually this is my first time I've used goto ever :)		default:	cout << endl << "Don't be funny, input a logical choice please!" << endl;					break;		}	}EndOfProgram:  //At least this's a bit clear	return 0;}void Rational::Simplify(){	for(int i = Denominator; i > 0; i--)	{		if(Numerator % i == 0 && Denominator % i == 0) //Check to see if it's possible to simplify		{			Numerator /= i;			Denominator /= i;		}	}	if(Denominator < 0)  //Normalizes the rational number	{		Denominator *= -1;		Numerator *= -1;	}}void Rational::operator = (Rational rhs){	Numerator = rhs.Numerator;	Denominator = rhs.Denominator;	Simplify();}Rational Rational::operator + (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Denominator + rhs.Numerator*Denominator);	temp.Denominator = (Denominator * rhs.Denominator);	temp.Simplify();	return temp;}Rational Rational::operator - (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Denominator - rhs.Numerator*Denominator);	temp.Denominator = (Denominator * rhs.Denominator);	temp.Simplify();	return temp;}Rational Rational::operator * (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Numerator);	temp.Denominator = (Denominator * rhs.Denominator);	temp.Simplify();	return temp;}Rational Rational::operator / (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Denominator);	temp.Denominator = (Denominator * rhs.Numerator);	temp.Simplify();	return temp;}ostream& operator << (ostream& bout, Rational& rhs){	bout << "Hi!";//rhs.Numerator << "/" << rhs.Denominator << endl;	return bout;}istream& operator >> (istream& bin, Rational& rhs){	while(true)	{		cout << endl << "Input numerator: ";		bin >> rhs.Numerator;		if(bin.good())		{			bin.ignore();			break;		}		cout << "Sigh, stop messing around please.  Try again." << endl;		bin.clear();		bin.ignore();	}	while(true)	{		cout << endl << "Input numerator: ";		bin >> rhs.Denominator;		if(bin.good())		{			bin.ignore();			if(rhs.Denominator == 0)				cout << "Sorry, denominator cannot be zero." << endl;			else				break;		}		cout << "Sigh, stop messing around please.  Try again." << endl;		bin.clear();		bin.ignore();	}	return bin;}


This is my complete code

Edit: Don't mock the goto statement :) I'm using it as a way to mock my teacher ;)
As you can see, I havn't completed the output and arthrimitic code yet in main(), due to this error.
Your code compiles on VS.Net 2003. That's odd. Maybe you should do a full rebuild?

By the way, using goto just to "mock your teacher" probably isn't a good idea.

EDIT: In case you're planning to ask, yes I uncommented rhs.Numerator << "/" << rhs.Denominator << endl;
Quote:Original post by load_bitmap_file
Your code compiles on VS.Net 2003. That's odd. Maybe you should do a full rebuild?

By the way, using goto just to "mock your teacher" probably isn't a good idea.


Thanks for testing it for me. Ok I'll change that goto stuff, thanks for the wise advice. I tend to make wisecracks.

Edit: I tried to rebuild it all, didn't work, will start a new project and see if that works.

Edit2: Nope, didn't work, still same errors...

[Edited by - Zeraan on September 5, 2005 6:30:06 PM]
Quote:Original post by Zeraan
Quote:Original post by load_bitmap_file
Your code compiles on VS.Net 2003. That's odd. Maybe you should do a full rebuild?

By the way, using goto just to "mock your teacher" probably isn't a good idea.


Thanks for testing it for me. Ok I'll change that goto stuff, thanks for the wise advice. I tend to make wisecracks.

Edit: I tried to rebuild it all, didn't work, will start a new project and see if that works.

Edit2: Nope, didn't work, still same errors...


What compiler are you using?
MS Visual C++ 6.0
Ok can someone do me a favor and test to make sure the program runs fine? I would do it myself if VC++ would work with it!

here's the code:

#include <iostream>using namespace std;class Rational{	int Numerator;	int Denominator;	void Simplify();public:	Rational() { Numerator = 0; Denominator = 1; }	Rational(int N, int D) { Numerator = N; Denominator = D; } //Constructor	void operator = (Rational);	Rational operator + (Rational);  //Basic arthrimtic functions	Rational operator - (Rational);	Rational operator * (Rational);	Rational operator / (Rational);		friend ostream& operator << (ostream&, Rational&); //iostream overloads	friend istream& operator >> (istream&, Rational&);};int main(){	bool Loop = true;	Rational R1(5,3); //Declare rationals with some numbers	Rational R2(6,2);	Rational Temp;	while(Loop)	{		int Choice = 0;		cout	<< endl << "Current Rationals: R1 - " << R1 << " and R2 - " << R2;		cout	<< endl << "Welcome to Zeraan's Rationalization Center!"				<< endl << "What would you like to do?  Input between 1 to 6:"				<< endl << "1. Input Rationals"				<< endl << "2. Add Rationals"				<< endl << "3. Subtract Rationals"				<< endl << "4. Multiply Rationals"				<< endl << "5. Divide Rationals"				<< endl << "6. Quit Rationalizing!"				<< endl << "Input your decision: ";		cin >> Choice;		switch(Choice)		{		case	1:	cin >> R1;					cin >> R2;					break;		case	2:	Temp = R1 + R2;						cout << endl << "The result of " << R1 << " + " << R2 << " is " << Temp << endl;					break;		case	3:	Temp = R1 - R2;					cout << endl << "The result of " << R1 << " - " << R2 << " is " << Temp << endl;					break;		case	4:	Temp = R1 * R2;					cout << endl << "The result of " << R1 << " * " << R2 << " is " << Temp << endl;					break;		case	5:	Temp = R1 / R2;					cout << endl << "The result of " << R1 << " / " << R2 << " is " << Temp << endl;					break;		case	6:	Loop = false;					break;		default:	cout << endl << "Don't be funny, input a logical choice please!" << endl;					break;		}	}	return 0;}void Rational::Simplify(){	for(int i = Denominator; i > 0; i--)	{		if(Numerator % i == 0 && Denominator % i == 0) //Check to see if it's possible to simplify		{			Numerator /= i;			Denominator /= i;		}	}	if(Denominator < 0)  //Normalizes the rational number	{		Denominator *= -1;		Numerator *= -1;	}}void Rational::operator = (Rational rhs){	Numerator = rhs.Numerator;	Denominator = rhs.Denominator;	Simplify();}Rational Rational::operator + (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Denominator + rhs.Numerator*Denominator);	temp.Denominator = (Denominator * rhs.Denominator);	temp.Simplify();	return temp;}Rational Rational::operator - (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Denominator - rhs.Numerator*Denominator);	temp.Denominator = (Denominator * rhs.Denominator);	temp.Simplify();	return temp;}Rational Rational::operator * (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Numerator);	temp.Denominator = (Denominator * rhs.Denominator);	temp.Simplify();	return temp;}Rational Rational::operator / (Rational rhs){	Rational temp; //Stores the info to be returned	temp.Numerator = (Numerator*rhs.Denominator);	temp.Denominator = (Denominator * rhs.Numerator);	temp.Simplify();	return temp;}ostream& operator << (ostream& bout, Rational& rhs){	bout << rhs.Numerator << "/" << rhs.Denominator;	return bout;}istream& operator >> (istream& bin, Rational& rhs){	while(true)	{		cout << endl << "Input numerator: ";		bin >> rhs.Numerator;		if(bin.good())		{			bin.ignore();			break;		}		cout << "Sigh, stop messing around please.  Try again." << endl;		bin.clear();		bin.ignore();	}	while(true)	{		cout << endl << "Input denominator: ";		bin >> rhs.Denominator;		if(bin.good())		{			bin.ignore();			if(rhs.Denominator == 0)				cout << "Sorry, denominator cannot be zero." << endl;			else				break;		}		cout << "Sigh, stop messing around please.  Try again." << endl;		bin.clear();		bin.ignore();	}	return bin;}


Thanks so much!
Sorry about OT - but did anyone see the topic title and think it was a bad joke?
In response to the previous AP: Only young immature minds who don't know what the hell the OT is referring to.

This topic is closed to new replies.

Advertisement