2016-11-13 10 views
-2

*演算子のオーバーロードを扱う他の多くの記事を見てきましたが、問題はまだ見えません。掛け算演算子のオーバーロード

#include <iostream> 
#include <string> 
#include <cmath> 

using namespace std; 


class Fraction{ 
    private: 
     int num, den; 
    public: 
     void set(int n, int d); 
     void setNum(int newNum); 
     void setDen(int newDen); 
     Fraction add(Fraction other); 
     Fraction simplify(); 
     Fraction(int new_Num = 0, int new_Den = 0){ 
      num = new_Num; 
      den = new_Den; 
     }; 
     void output(Fraction& f); 
     void cleanerFunction(Fraction& f); 
     int gcd(); 
     Fraction reduce(); 
     friend ostream& operator <<(ostream& os, Fraction& f); 
     friend istream& operator >>(istream& in, Fraction& f); 
     friend Fraction operator + (const Fraction&, const Fraction&); 
     friend inline Fraction operator - (const Fraction&, const Fraction&); 
     friend Fraction operator * (const Fraction&, const Fraction&); 
     friend Fraction operator/(const Fraction&, const Fraction&); 
     friend bool operator < (const Fraction&, const Fraction&); 
     friend bool operator <= (const Fraction&, const Fraction&); 
     friend bool operator > (const Fraction&, const Fraction&); 
     friend bool operator >= (const Fraction&, const Fraction&); 
     friend bool operator == (const Fraction&, const Fraction&); 

}; 
int gcd(int x, int y); 
Fraction reduce(Fraction f); 
void reduce(Fraction& f); 

Fraction operator + (const Fraction& f, const Fraction& r){ 
    int temp_Num, temp_Den; 
    temp_Num = (f.num * r.den) + (f.den * r.num); 
    temp_Den = (f.den * r.den); 
    Fraction temp_Frac(temp_Num, temp_Den); 
    temp_Frac.cleanerFunction(temp_Frac); 
    return temp_Frac; 

}; 
Fraction operator - (const Fraction& f, const Fraction& r){ 
    int temp_Num, temp_Den; 
    temp_Num = (f.num * r.den) - (f.den * r.num); 
    temp_Den = (f.den * r.den); 
    Fraction temp_Frac(temp_Num, temp_Den); 
    temp_Frac.cleanerFunction(temp_Frac); 
    return temp_Frac; 

}; 
Fraction operator * (const Fraction& f, const Fraction& r){ 
    int temp_Num, temp_Den; 
    temp_Num = (f.num * r.num); 
    temp_Den = (f.den * r.den); 
    Fraction temp_Frac(temp_Num, temp_Den); 
    temp_Frac.cleanerFunction(temp_Frac); 
    return temp_Frac; 
}; 
Fraction operator/(const Fraction& f, const Fraction& r){ 
    int temp_Num, temp_Den; 
    temp_Num = (f.num * r.den); 
    temp_Den = (f.den * r.num); 
    Fraction temp_Frac(temp_Num, temp_Den); 
    return temp_Frac; 

}; 

bool operator < (const Fraction& f, const Fraction& r){ 
    return (f.num * r.den) < (r.num * f.den); 
}; 
bool operator <= (const Fraction& f, const Fraction& r){ 
    return (f.num * r.den) <= (r.num * f.den); 
}; 
bool operator > (const Fraction& f, const Fraction& r){ 
    return (f.num * r.den) > (r.num * f.den); 
}; 
bool operator >= (const Fraction& f, const Fraction& r){ 
    return (f.num * r.den) >= (r.num * f.den); 
}; 
bool operator == (const Fraction& f, const Fraction& r){ 
    return (f.num * r.den) == (r.num * f.den); 
}; 


ostream& operator <<(ostream& os, Fraction& f) 
{ 
    f.output(f); 
    return os; 
} 

istream& operator >>(istream& in, Fraction& f) 
{ 
    char ch; 
    int temp_Num, temp_Den; 
    in >> temp_Num >> ch >> temp_Den; 
    f.set(temp_Num, temp_Den); 
    f.cleanerFunction(f); 
    return in; 
} 

int main() 
{ 
    cout <<"HELLO\n"; 
    cout << "Testing declarations " << endl; 
    cout << "Fraction x, y(2), z(-5,-6), w(1,-3);" << endl; 
    Fraction x, y(2), z(-5,-6), w(1,-3); 
    cout << "z = " << z << ", y = " << y << ", z = " << z << ", w = " << w << endl; 
    cout << "Testing >> overloading: \nEnter a fraction in the format " << "integer _numerator/integer _denominator" << endl; 
    cin >> x; 
    cout << "You entered the equivalent of: " << x << endl; 
    //cout << z << " - (" << w << ") = " << z - w << endl; 
    cout << "Testing the constructor and normalization routines: " << endl; 
    y =Fraction(-128, -48); 
    cout << "y =Fraction(-128, -48) outputs as " << y << endl; 
    y =Fraction(-128, 48); 
    cout << "y =Fraction(-128, 48) outputs as " << y << endl; 
    y = Fraction(128,-48); 
    cout << "y = Fraction(128, -48) outputs as " << y << endl; 
    Fraction a(1,1); 
    cout << "Fraction a(1,1); a outputs as: " << a << endl; 
    Fraction ww = y*a; 
    cout << y << " * " << a << " = " << ww << endl; 
    w = Fraction(25,9); 
    z = Fraction(3,5); 
    cout << "Testing arithmetic and relational operator overloading" << endl; 
    //cout << w << " * " << z << " = " << w * z << endl; 
    //cout << w << " + " << z << " = " << w + z << endl; 
    //cout << w << " - " << z << " = " << w - z << endl; 
    //cout << w << "/" << z << " = " << w/z << endl; 
    cout << w << " < " << z << " = " << (w < z) << endl; 
    cout << w << " < " << w << " = " << (w < w) << endl; 
    cout << w << " <= " << z << " = " << (w <= z) << endl; 
    cout << w << " <= " << w << " = " << (w <= w) << endl; 
    cout << w << " > " << z << " = " << (w > z) << endl; 
    cout << w << " > " << w << " = " << (w > w) << endl; 
    cout << w << " >= " << z << " = " << (w >= z) << endl; 
    cout << w << " >= " << w << " = " << (w >= w) << endl; 
    w = Fraction(-21,9); 
    z = Fraction(3,5); 
    //cout << w << " * " << z << " = " << w * z << endl; 
    //cout << w << " + " << z << " = " << w + z << endl; 
    //cout << w << " - " << z << " = " << w - z << endl; 
    //cout << w << "/" << z << " = " << w/z << endl; 
    cout << w << " < " << z << " = " << (w < z) << endl; 
    cout << w << " < " << w << " = " << (w < w) << endl; 
    cout << w << " <= " << z << " = " << (w <= z) << endl; 
    cout << w << " <= " << w << " = " << (w <= w) << endl; 
    cout << w << " > " << z << " = " << (w > z) << endl; 
    cout << w << " > " << w << " = " << (w > w) << endl; 
    cout << w << " >= " << z << " = " << (w >= z) << endl; 
    cout << w << " >= " << w << " = " << (w >= w) << endl; 
    return 0; 
    return 0; 
} 

Fraction Fraction::add(Fraction other){ 
    Fraction result; 
    result.num = num*other.den + other.num *den; 
    result.den = den*other.den; 
    return result; 

} 


Fraction Fraction::simplify(){ 
    Fraction f1; 
    f1.num = 4; f1.den=5; 
    return f1; 

} 

int gcd(int x, int y){ 
    if(y<0) 
     y =-y; 

    if(x % y == 0) 
     return y; 
    else 
     return gcd(y, x%y); 
} 

int Fraction::gcd(){ 
    Fraction temp; 
    if(den<0) 
      den =-den; 

     if(num % den == 0) 
      return den; 
     else{ 
      temp.num =den; 
      temp.den= num%den; 
      return temp.gcd(); 
     } 
} 

void Fraction::set(int n, int d){ 
    num = n; 
    den = d; 

} 

void Fraction::setNum(int newNum){ 
    num = newNum; 
} 
void Fraction::setDen(int newDen){ 
    den = newDen; 
} 
Fraction Fraction::reduce(){ 
    Fraction temp; 
    temp.set(num,den); 
    int m = temp.gcd(); 
    num /= m; 
    den /= m; 
} 
Fraction reduce(Fraction f){ 
    int m = f.gcd(); 

} 
void reduce(Fraction& f){ 

} 

void Fraction::output(Fraction& f){ 

    f.cleanerFunction(f); 
    cout << num << "/"<< den; 
} 

void Fraction::cleanerFunction(Fraction& f){ 
    int temp_Num = num; 
    int temp_Den = den; 

    if ((temp_Num != 0) && (temp_Den == 0)) 
     temp_Den = 1; 
    if ((temp_Num > 0) && (temp_Den < 0)){ 
     temp_Den = abs(temp_Den); 
     temp_Num = -temp_Num; 
    }; 
    if ((temp_Num < 0) && (temp_Den < 0)){ 
     temp_Den = abs(temp_Den); 
     temp_Num = abs(temp_Num); 
    }; 

    num = temp_Num; 
    den = temp_Den; 
} 

さらにコードを投稿する必要がある場合は、関連するコードがあります。コード* cout < < w * z < < endl; *、Main()のすべてを変更することはできません。私が手

エラーが エラーです: 'にはstd :: ostreamに{別名のstd :: basic_ostream}' 左辺値をバインドすることはできませんメインの 'STD :: basic_ostream & &'

コメントアウト行()はエラーメッセージを受け取る行です。だから私は(+、 - 、*、/)演算子に問題が発生します。私はちょうど*を見たかったので、戻って残りの部分に同じ修正を適用することができました。

+2

問題が何でありますか?エラーメッセージが表示されますか?間違った結果? 'cleanerFunction'の定義はどこですか? – user463035818

+0

私はより多くの情報を追加します。現在のところ、プログラムの他の多くの部分で機能するので、クリーナー機能に問題はありませんが、私はすべてを追加します。 – Juscallmesteve

+0

_ "と私はまだ問題を見ることができませんでした。" _私たちも問題を何か教えてください。 –

答えて

1

cout << w * z << endl;のエラーメッセージは、operator<<のオーバーロードがなく、クラスFractionを引数に取るためです。このオーバーロードを定義してみてください。

std::ostream& operator<<(std::ostream& os, const Fraction& f) 
{ 
    return os << f.den << "/" << f.num; 
} 

そして、この機能にdennumを利用できるようにするためにclass Fractionの定義に次の行を追加します。

friend std::ostream& operator<<(std::ostream& os, const Fraction& f); 
+0

私はそれが過負荷になっている、私はちょうどこのポストに含めることが重要だと思う。しかし、私は今、全プログラムを含めました。 – Juscallmesteve

+1

@Juscallmesteveあなたは 'Fraction&'(左辺値)をとるオーバーロードを持っています。 'const Fraction&'を取るように変更してください。 –

+0

ok悪いところから – Juscallmesteve

関連する問題