我想我在这段代码上工作太久了。无论如何,这是正在发生的事情。
头文件(项目范围禁止更改公开)
#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
我正在尝试重载 + 运算符,这是我的代码:
Fraction operator+(const Fraction &f1, const Fraction &f2)
{
return(((f1.num*f2.denom)+(f1.denom*f2.num)),(f1.denom*f2.denom));
}
我收到关于将 num 和 denom 作为私有变量引用的错误,我只是很难弄清楚如何纠正这个问题。