C++ 在表达式中组合运算符重载

C++ combining operator overloads in expression

问题

我正在写一个分数 class,其运算符已重载。我的问题是,当我尝试组合两个重载表达式时,例如组合 post 增量 ++ 和 ostream <<,编译器抛出错误。

工作测试

以下测试用例完美运行 - 组合重载是问题所在

Fraction testFraction(1,2);
Fraction anotherFraction = testFraction ++;
Fraction yetAnotherFraction = - anotherFraction;
Fraction woahSoManyFractions = anotherFraction + yetAnotherFraction;

示例 1

Fraction testFraction(1,3)
cout << testFraction++;

示例 2

Fraction testFraction(1,6);
cout << "Should be \"-1/6\": " << -testFraction << endl;

示例 3

 Fraction test14 = 2 + test3++;

头文件

#pragma once
#include <string>
#include <iostream>
using namespace std;
//Fractions Class
class Fraction{
public:
    Fraction();
    Fraction(int wholeNum);
    Fraction(int numer, int denom);
    int getNumerator();
    int getDenominator();
    Fraction operator - ();    // Member Overloading of unary -
    Fraction  operator ++ ();    // Member Overloading of prefix ++
    Fraction  operator ++ (int);    // Member Overloading of postfix ++

    Fraction operator + (const Fraction& rhs) const; //Member Overloading of binary + (Fraction + Fraction)
    Fraction operator - (const Fraction& rhs) const; //Member Overloading of binary - (Fraction - Fraction)
    Fraction operator * (const Fraction& rhs) const; //Member Overloading of binary * (Fraction * Fraction)
    Fraction operator / (const Fraction& rhs) const; //Member Overloading of binary / (Fraction / Fraction)

    friend ostream& operator << (ostream& os, Fraction& fr);
private:
    int numerator;
    int denominator;


};
//Non-member overloads
Fraction operator + (int num, Fraction &rhs); //non-member Overloading of binary + (Num + Fraction) or (Fraction + Num)
Fraction operator - (int num, Fraction &rhs); //non-member Overloading of binary - (Num - Fraction) or (Fraction - Num)
Fraction operator * (int num, Fraction &rhs); //non-member Overloading of binary * (Num * Fraction) or (Fraction * Num)

//GCD Helper Function
int findGCD(int num, int denom); // update num and denom values passed by reference to normalized array

//Custom exception class to division by zero fractions
class FractionException {
public:
    FractionException(const string&);
    string what() const;
private:
    string message;
};

实现 C++ 文件

#include "fraction_14cc78.h"
#include <string>
#include <iostream>
using namespace std;
// ---------------- Exceptions ---------------------
FractionException::FractionException(const string& m){
    message = m;
}

string FractionException::what() const {
    return message;
}
// ------------------------------ Fraction Class ----------------------------
//----- << Overload-----
ostream& operator<<(ostream& os, Fraction& fr)
{
    os << fr.getNumerator() << '/' << fr.getDenominator() << endl;
    return os;
}
//----- Unary (-) Overload----
Fraction Fraction::operator -(){
    return Fraction(-numerator, denominator);
}
//----- Prefix ++ Overload----
Fraction Fraction::operator ++ (){;
//increment object
    return Fraction(numerator + denominator, denominator);
}
//----- Postfix ++ Overload----
Fraction Fraction::operator ++ (int){
    // save the orignal value
    Fraction F(numerator, denominator);
    // increment object
    numerator = numerator + denominator;
    // return old original value
    return F;
}
//----- Binary + Overload (Fraction + Fraction) ----
Fraction Fraction::operator + (const Fraction& rhs) const{
    return Fraction((numerator * rhs.denominator) + (rhs.numerator * denominator), (denominator * rhs.denominator));
}
//----- Binary + Overload (num + Fraction) ----
Fraction operator + (int num, Fraction &rhs){
    return Fraction((rhs.getDenominator() * num) + rhs.getNumerator(), rhs.getDenominator());
}
//----- Binary - Overload (Fraction - Fraction) ----
Fraction Fraction::operator - (const Fraction& rhs) const{
    return Fraction((numerator * rhs.denominator) - (rhs.numerator * denominator), (denominator * rhs.denominator));
}
//----- Binary - Overload (num - Fraction) ----
Fraction operator - (int num, Fraction &rhs){
    return Fraction((rhs.getDenominator() * num) - rhs.getNumerator(), rhs.getDenominator());
}
//----- Binary * Overload (Fraction * Fraction) ----
Fraction Fraction::operator * (const Fraction& rhs) const{
    return Fraction((numerator * rhs.numerator), (denominator * rhs.denominator));
}
//----- Binary * Overload (num * Fraction) ----
Fraction operator * (int num, Fraction &rhs){
    return Fraction((rhs.getNumerator() * num), rhs.getDenominator());
}
//----- Binary / Overload (Fraction / Fraction) ----
Fraction Fraction::operator / (const Fraction& rhs) const{
    return Fraction((numerator * rhs.denominator), (denominator * rhs.numerator));
}



// ---------------- Constructors------------------
//TODO normalize fractions and find greates common denom in this constructor
Fraction::Fraction(int num, int denom){
    //Handle 0 denom case
    if(denom == 0){
        throw FractionException("Cannot create fraction with zero denominator");
    }
    //Find GCD
    int GCD = findGCD(abs(num), abs(denom));
    num = num / GCD;
    denom = denom / GCD;
    //Handle Negative Fractions
    if(num < 0 && denom < 0){
        num = num * -1;
        denom = denom * -1;
    }else if(num < 0 || denom < 0){
        num = abs(num) * -1;
        denom = abs(denom);
    }
    numerator = num;
    denominator = denom;
    //cout << "Fraction Created: " << numerator << "/" << denominator <<endl;
}
Fraction::Fraction(int num){
    numerator = num;
    denominator = 1;
    //cout << "Fraction Created: " << numerator << "/" << denominator <<endl;
}
Fraction::Fraction(){
    numerator = 0;
    denominator = 1;
    //cout << "Fraction Created: " << numerator << "/" << denominator <<endl;
}
// ---------------- Accessors ------------------
int Fraction::getNumerator(){
    return numerator;
}
int Fraction::getDenominator(){
    return denominator;
}
// ---------------- GCD Helper Function ------------------
int findGCD(int num, int denom){
    if((denom <= num) && (num % denom == 0))
        return denom;
    else if(num < denom){
        return findGCD(denom, num);
    } else{
        return findGCD(denom, num % denom);
    }
}
ostream& operator << (ostream& os, Fraction& fr);
Fraction operator + (int num, Fraction &rhs);
Fraction operator - (int num, Fraction &rhs);
Fraction operator * (int num, Fraction &rhs);

应该都把Fraction作为const引用吧!标准不允许引用临时文件(即使 msvc 编译此类代码...)。

编辑:

头文件更新部分:

#pragma once
#include <string>
#include <iostream>
using namespace std;
//Fractions Class
class Fraction{
    public:
    Fraction();
    Fraction(int wholeNum);
    Fraction(int numer, int denom);
    int getNumerator();
    int getDenominator();
    Fraction operator - ();    // Member Overloading of unary -
    Fraction  operator ++ ();    // Member Overloading of prefix ++
    Fraction  operator ++ (int);    // Member Overloading of postfix ++

    Fraction operator + (const Fraction& rhs) const;
    Fraction operator - (const Fraction& rhs) const;
    Fraction operator * (const Fraction& rhs) const;
    Fraction operator / (const Fraction& rhs) const;

    friend ostream& operator << (ostream& os, const Fraction& fr);
private:
    int numerator;
    int denominator;
};
//Non-member overloads
Fraction operator + (int num, const Fraction &rhs);
Fraction operator - (int num, const Fraction &rhs);
Fraction operator * (int num, const Fraction &rhs);

和改编的实现代码:

//----- << Overload-----
ostream& operator<<(ostream& os, const Fraction& fr)
{
    os << fr.getNumerator() << '/' << fr.getDenominator() << endl;
    return os;
}
//----- Binary + Overload (num + Fraction) ----
Fraction operator + (int num, const Fraction &rhs){
    return Fraction((rhs.getDenominator() * num) + rhs.getNumerator(), rhs.getDenominator());
}
//----- Binary - Overload (num - Fraction) ----
Fraction operator - (int num, const Fraction &rhs){
     return Fraction((rhs.getDenominator() * num) - rhs.getNumerator(), 
rhs.getDenominator());
}
//----- Binary * Overload (num * Fraction) ----
Fraction operator * (int num, const Fraction &rhs){
    return Fraction((rhs.getNumerator() * num), rhs.getDenominator());
}