2013-05-05 187 views
0

家庭作業重載運算符<< operator ==和operator!=

有超載兩種操作< <,operator ==和operator!=

h文件和.cpp文件包含以下:

namespace JoePitz 
{ 
    class Complex 
    { 
     // declare friend functions 
     friend ostream &operator<<(ostream &out, const Complex &value); 
     friend ostream &operator<<(ostream &out, const bool &value); 
     friend istream &operator>>(istream &in, Complex &value); 

     public: 
     // constructor 
    Complex(double real, double imaginary); 

     // overloading +/-/==/!= operators 
     Complex operator+(const Complex &compx2); 
     Complex operator-(const Complex &compx2); 
     bool operator==(const Complex &compx2); 
     bool operator!=(const Complex &compx2); 

     private: 
     double real; 
     double imaginary; 
     void initialize(double real, double imaginary); 
    }; 

     // GCC requires friend functions to be declared in name space 
     ostream &operator<<(ostream &out, const Complex &value); 
     ostream &operator<<(ostream &out, const bool &value); 
     istream &operator>>(istream &in, Complex &value); 
} 

    excerpt from .cpp file 

    ostream& JoePitz::operator<<(ostream &out, const Complex &value) 
    { 
     // print real 
     cout << value.real; 

    // print imaginary 
    if (value.imaginary == ISZERO) 
    { 
     cout << POSSIGN << value.imaginary << IMAGNSGN; 
    } 
    else if (value.imaginary > ISZERO) 
    { 
     cout << POSSIGN << value.imaginary << IMAGNSGN; 
    } 
    else 
    { 
     cout << value.imaginary << IMAGNSGN; 
    } 

    return out; 

    } 

    ostream& JoePitz::operator<<(ostream &out, const bool &value) 
    { 
    return out; 
    } 

    // overloaded == operator 
    bool JoePitz::Complex::operator==(const Complex &compx2) 
    { 
    return (this->real == compx2.real && this->imaginary == compx2.imaginary); 
    } 

    // overloaded != operator 
    bool JoePitz::Complex::operator!=(const Complex &compx2) 
    { 
    return !(this->real == compx2.real && this->imaginary == compx2.imaginary); 
    } 

我收到以下編譯錯誤:

../src/hw4.cpp:71:錯誤:不對應的「運營商<「的 'C1 < < 」* * * \ 012「' ../src/Complex.h:54 <:注意:考生有:性病:: ostream的& JoePitz ::運算< <(STD :: ostream的& ,常量布爾&) ../src/Complex.h:53:注意:性病:: ostream的& JoePitz ::運算< <(STD :: ostream的&,常量JoePitz ::複雜&)

從我理解這是由於不知道要實現哪個重載函數所致。

我遇到的問題是如何處理運算符函數返回一個ostream並接受一個Complex對象的事實,但運算符==函數返回一個布爾值。

但我不知道如何更改運算符==函數來處理布爾和或Complex對象。我試圖添加另一個超載操作符< <函數返回一個布爾,但編譯器仍然有問題。

任何援助將不勝感激。

+0

哪裏錯誤發生? – chris 2013-05-05 02:57:49

+0

什麼是POSSIGN/IMAGNSGN的類型/值?此外,目前還不清楚爲什麼你爲操作符<<重載ostream/bool對(特別是假設你的重載沒有任何作用)。 – huskerchad 2013-05-05 03:04:40

+0

請分享實際產生錯誤的代碼。錯誤消息的行號與您發佈的樣本不符。更好的是,發佈[SSCCE](http://sscce.org/)。 – japreiss 2013-05-05 03:17:46

回答

2

這是一個簡單的運算符優先級的錯誤,這

cout << "* * * Unit Test 3 comparison operations == != * * \n"; 
cout << " * * Print c1 == c1 = " << c1 == c1 << " * * *\n"; 

應該是這個

cout << "* * * Unit Test 3 comparison operations == != * * \n"; 
cout << " * * Print c1 == c1 = " << (c1 == c1) << " * * *\n"; 

<<的優先級高於==所以你需要括號。

刪除ostream bool過載。

另一個變化

Complex operator+(const Complex &compx2); 
    Complex operator-(const Complex &compx2); 
    bool operator==(const Complex &compx2); 
    bool operator!=(const Complex &compx2); 

都應該是const方法

Complex operator+(const Complex &compx2) const; 
    Complex operator-(const Complex &compx2) const; 
    bool operator==(const Complex &compx2) const; 
    bool operator!=(const Complex &compx2) const;