0

I'm writing code that uses friend functions but I am not sure why I get the error "is a private member of" in the function "sum" since I declared the function as a friend in the header file.

Header File:

#include <iostream>

class rational
{
public:

    // ToDo: Constructor that takes int numerator and int denominator
    rational (int numerator = 0, int denominator = 1);
   // ToDo: Member function to write a rational as n/d
    void set (int set_numerator, int set_denominator);
    // ToDo: declare an accessor function to get the numerator
    int  getNumerator () const;
    // ToDo: declare an accessor function to get the denominator
    int  getDenominator () const;
    // ToDo: declare a function called Sum that takes two rational objects
    // sets the current object to the sum of the given objects using the
    // formula: a/b + c/d = ( a*d + b*c)/(b*d)
    friend rational sum (const rational& r1, const rational& r2);

    void output (std::ostream& out);
    // member function to display the object

    void input (std::istream& in);


private:

    int numerator;
    int denominator;

};

Source File:

#include <iostream>
using namespace std;


//  takes two rational objects and uses the formula a/b + c/d = ( a*d + b*c)/(b*d) to change the numerator and denominator


rational sum (rational r1, rational r2)
{
    // formula: a/b + c/d = ( a*d + b*c)/(b*d)

    cout << endl;

    numerator = ((r2.denominator * r1.numerator) + (r1.denominator * r2.numerator));

    denominator = (r1.denominator * r2.denominator);
}
  • 3
    `rational sum (const rational& r1, const rational& r2);` and `rational sum (rational r1, rational r2)` are two different functions, their parameter declarations don't match – UnholySheep Apr 07 '19 at 17:18
  • 1
    If you have "getter" functions like `getNumerator` and `getDenominator`, why do you need to make the `sum` function a friend? Friend functions usually complicates things, tend to make code messier and less maintainable. There are exceptions of course (the input and output operator overloads normally being one of them), but in general try to avoid them if you can. – Some programmer dude Apr 07 '19 at 17:25
  • 1
    I think your declaration needs to say `rational rational::sum (rational r1, rational r2)` – Tetix Apr 07 '19 at 17:44

2 Answers2

1

rational sum (rational r1, rational r2) is a totally new function (no way to relate to the class rational) that accepts two rationals and returns a rational.

The correct way to implement the needed class method would be rational rational::sum (const rational& r1, const rational& r2)

Overall comment: Use capitalized first letter for classes (Rational)

Tetix
  • 317
  • 2
  • 10
  • the stated purpose was to use a friend declaration, so the global function should be `rational sum (const rational& r1, const rational& r2)` instead –  Apr 07 '19 at 18:04
  • Get an error "Use of undeclared identifier 'numerator'' when I change it to your suggested implementation. (rational sum (const rational& r1, const rational& r2)) – silentchaos Apr 07 '19 at 18:07
  • @user10793479 of course, notice the extra `int`s in my answer –  Apr 07 '19 at 18:18
0

You want something like this:

rational sum (const rational& r1, const rational& r2)
{
    // formula: a/b + c/d = ( a*d + b*c)/(b*d)

    int numerator = ((r2.denominator * r1.numerator) + (r1.denominator * r2.numerator));

    int denominator = (r1.denominator * r2.denominator);
    return rational(numerator, denominator);
}