I think I have been working on this code too long. Anyway here is whats happening.
header file (scope of the project forbids changing public)
#ifndef FRACTION_
#define FRACTION_
using namespace std;
#include <iostream>
class Fraction
{
private:
int num,denom;
public:
// Construct fraction from numerator and denominator
//
Fraction( int = 0, int = 1 );
// Construct fraction by copying existing fraction
//
Fraction( const Fraction& );
// Assign into fraction by copying existing fraction
//
Fraction& operator=( const Fraction& );
// Return true if fraction is valid (non-zero denominator)
//
bool IsValid() const;
// Return value of numerator
//
int Numerator() const;
// Return value of denominator
//
int Denominator() const;
// Input/Output operations
//
friend istream& operator>>( istream&, Fraction& );
friend ostream& operator<<( ostream&, const Fraction& );
};
// Comparative operations
//
bool operator==( const Fraction&, const Fraction& );
bool operator!=( const Fraction&, const Fraction& );
bool operator< ( const Fraction&, const Fraction& );
bool operator<=( const Fraction&, const Fraction& );
bool operator> ( const Fraction&, const Fraction& );
bool operator>=( const Fraction&, const Fraction& );
// Arithmetic operations
//
Fraction operator+( const Fraction&, const Fraction& );
Fraction operator-( const Fraction&, const Fraction& );
Fraction operator*( const Fraction&, const Fraction& );
Fraction operator/( const Fraction&, const Fraction& );
#endif
I am trying to overload the + operator, here is my code for that:
Fraction operator+(const Fraction &f1, const Fraction &f2)
{
return(((f1.num*f2.denom)+(f1.denom*f2.num)),(f1.denom*f2.denom));
}
I get an error about referencing num and denom as private variable, I am just having a hard time figuring out how to correct that problem.
Use Numerator()
instead of num
and Denominator()
instead of denom
. Your operator is not a member, nor a friend, so it can't access private members.
The other option would be to
5 + frac
, only frac + 5
) friend
, (but making it a friend when it doesn't have to be increases the number of things that have access to the class not through its interface, leading to increased code maintenance)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With