Sign in to follow this  
Zeraan

Problem with being friends with another class

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry about OT - but did anyone see the topic title and think it was a bad joke?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In response to the previous AP: Only young immature minds who don't know what the hell the OT is referring to.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I've found VC 6 to have issues with some C++ stuff. That's why I paid the $19 to get my "educational" *cough* unlimited *cough* edition of 2003 from school. You can do the same if you go to the book store (on campus) and show them your school ID and enjoy the savings!

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Sorry about OT - but did anyone see the topic title and think it was a bad joke?


Oops, my bad! Sorry about that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zeraan
MS Visual C++ 6.0


Your code compiles fine on my MS Visual C++ 6.0. Did you install the latest service pack?

Tom

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I've found VC 6 to have issues with some C++ stuff. That's why I paid the $19 to get my "educational" *cough* unlimited *cough* edition of 2003 from school. You can do the same if you go to the book store (on campus) and show them your school ID and enjoy the savings!


Holy crap $19??!!



I only paid $15 for mine [wink]. The deal kind of blows me away, considering the lack of protection on the terms of the agreement practically screams from misuse from those who are most likely to misuse it in the first place...Of course then again, they do get many of the people who would completely pirate the software to pay something for it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this