2017-04-18 2 views
-1

演算子のオーバーロードでフレンド関数を実装する際に問題が発生しています。私はこれらの機能を使うために何が必要かをしっかりと理解していません。誰かがどのように進むべきかについて私にいくつかの指導を与えることができます。どうかありがとうございます。 手順:演算子とフレンド関数のオーバーロード

This class implements rational number of the type 2/3. 

class Rational; 

private data: int n, (fraction numerator) 
int d (fraction denominator). 

// make sure that d is always positive. 

// optional: always simplify the number so that n and d don't have a common factor 


public interface: 
constructors: 
two int args, to allow setting rational to any legal value 
default numerator to be 0, default denominator to be 1 

friend istream& operator >>(istream& in_str, Rational& right) 

friend ostream& operator <<(ostream& out_str, cnst Rational& right) 



overload + - */< <= > >= == 

friend Rational operator+(const Rational&, const Rational&); 
.... 


*/ 
/* 
A possible test and output is given below. 

int main() 
{ 
cout << "Testing declarations" << endl; 
cout << "Rational x, y(2), z(-5,-6), w(1,-3);" << endl; 
Rational x, y(2), z(-5, -6), w(1, -3); 
cout << "x = " << x << ", y = " << y << ", z = " << z 
<< ", w = " << w << endl << endl; 



cout << "Testing the constructor and normalization routines: " << endl; 
y = Rational(-1, -4); 
cout << "y =Rational(-1, -4) outputs as " << y << endl; 
y = Rational(-1, 4); 
cout << "y =Rational(-1, 4)outputs as " << y << endl; 
y = Rational(1, -4); 
cout << "y = Rational(128, -48) outputs as " << y << endl; 
Rational a(1, 1); 
cout << "Rational a(1,1); a outputs as: " << a << endl; 
Rational ww = y*a; 
cout << y << " * " << a << " = " << ww << endl << 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 << endl; 



w = Rational(-7, 3); 
z = Rational(-3, 5); 
cout << "Testing arithmetic and relational " 
<< " operator overloading" << endl << 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 << " <= " << 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; 

return 0; 
} 

== == == == == == == == == 

TestingTesting declarations 
Rational x, y(2), z(-5, -6), w(1, -3); 
x = 0/1, y = 2/1, z = 5/6, w = -1/3 

Testing the constructor and normalization routines : 
y = Rational(-1, -4) outputs as 1/4 
y = Rational(-1, 4)outputs as - 1/4 
y = Rational(128, -48) outputs as - 1/4 
Rational a(1, 1); a outputs as : 1/1 
- 1/4 * 1/1 = -1/4 

Testing >> overloading : 
    Enter a fraction in the format integer_numerator/integer_denominator 
    2/-3 
    You entered the equivalent of : -2/3 
    5/6 - (-1/3) = 21/18 

    Testing arithmetic and relational operator overloading 

    - 7/3 * -3/5 = 21/15 
    - 7/3 + -3/5 = -44/15 
    - 7/3 - -3/5 = -26/15 
    - 7/3/-3/5 = 35/9 
    - 7/3 < -3/5 : 1 
    - 7/3 <= -3/5 : 1 
    - 7/3 <= -7/3 : 1 
    - 7/3 > -3/5 : 0 
    - 7/3 > -7/3 : 0 
    - 7/3 >= -3/5 : 0 

*/ 

マイコード:

#include <iostream> 
using namespace std; 

class Rational { 
    private: 
     int n; // (fraction numerator) 
     int d; // (fraction denominator). 

    public: 
     Rational() { 
      n = 0; 
      d = 1; 
     } 
     Rational(int num, int denom) { 
      n = num; 
      d = denom; 
     } 

     friend istream& operator >> (istream& in_str, Rational& right); 
     friend ostream& operator << (ostream& out_str, const Rational& right); 
     friend Rational operator+ (const Rational&, const Rational&); 

     //friend istream& operator >> (istream& in_str, Rational& right); 
     //friend ostream& operator <<(ostream& out_str, const Rational& right); 
     //friend Rational operator+(const Rational&, const Rational&); 


}; 




int main() 
{ 
    cout << "Testing declarations" << endl; 
    cout << "Rational x, y(2), z(-5,-6), w(1,-3);" << endl; 
    return 0; 
} 

istream& operator >> (istream& in_str, Rational& right) { 
    cin >> n.right; 
} 
ostream& operator << (ostream& out_str, const Rational& right); { 
    cout << out_str; 
} 
Rational operator+(const Rational&, const Rational&); { 
    cout << Rational; 
} 
+1

*「オーバーロード演算子で友人機能を実装する際に問題があります」* - 何が問題なのですか?申し訳ありません...私たちはここでは魔法使いではありません。 :-) [How to ask](https://stackoverflow.com/help/how-to-ask)を参照してください。 – WhiZTiM

+0

私は良いテキストブックを見て、例を通して作業することをお勧めします。それは、これらの特定の質問に答えを出すよりも、長期的にはあなたにもっと役立つでしょう。 –

答えて

0

はそれをこのように考えて....あなたの例では、あなたが本当にあなたのクラスのメソッドをオーバーロードされるのではなく、あなたがオーバーロードしていますストリームの場合は演算子>>を使用します。 あなたは

istream& operator >> (istream& in_str, Rational& right) 

およびその他のストリーム演算子の実装は、あなたのクラスのメンバーデータを見ることができるように、その友人ようにする必要があります。あなたのクラスのメソッドではなく、グローバル関数として定義されています。彼らが友人機能でなければ、彼らは彼らと仕事をする必要がある私的なデータを見ることができませんでした。

これは、キーワード「友人」を使用して許容に近いだろう非常に数回の一つである。https://isocpp.org/wiki/faq/friendshttps://isocpp.org/wiki/faq/operator-overloading

を参照してください。 「友人」は、一般的にすべてのケースで嫌われますが、これは工場パターンを実装するときです。

対照的に、Googleの「C++オーバーロード演算子=」はクラスメソッドであるため、友人である必要はありません。

関連する問題