Parameter confusion with class objects and overloaded operators

Hi,

I have a friend function that overloads the insertion operator to deal with class objects.

I'm confused because I'm not sure how it takes its parameter.

Here's the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  std::ostream& operator<<(std::ostream &stream, Rational rhs){
    
    stream << rhs.numerator << "/" << rhs.denominator ;
    
    return stream;
    
} 

// one of my overloaded operator functions..

Rational Rational::operator+(int rhs){

    int tempN = (rhs * denominator) + numerator;
    int tempD = denominator;

    return Rational(tempN, tempD);//if this is my return, how is it passed in

}


Is my return in the + function incorrect, or can that work somehow?

Thank you.

**edit
I should point out that I can't mess with the declaration of the functions. Just the code inside.
Last edited on
I don't understand your question

> I'm not sure how it takes its parameter.
std::ostream& operator<<(std::ostream &stream, Rational rhs)
`rhs' is passed by copy, ¿is that what you're asking?

> t I can't mess with the implementation of the functions. Just the code inside.
the implementation is the code inside...
I fixed the implementation error. Thank you.

My question is how do I call the friend function inside that + operator function.
1
2
std::cout << *this;
std::cout << Rational(tempN, tempD);
Topic archived. No new replies allowed.