2013-12-19 107 views
1

所以我遇到的問題是在我的c + +程序,當我嘗試和測試我重載的分配運算符。問題與重載分配運算符

聲明:

cm6 = cm5; 

應該設置CM6等於CM5,但CM6的價值不會改變。這裏是我的代碼:


Proj_11.h

#include <iostream> 
#include <string> 
#include <iomanip> 

using namespace std; 

class ComplexNumber 
{ 
    private: 
     double _real; 
     double _imaginary; 
    public: 
     ComplexNumber(); 
     ComplexNumber(double, double); 
     ComplexNumber(ComplexNumber&); 
     ComplexNumber operator+ (ComplexNumber); 
     ComplexNumber operator- (ComplexNumber); 
     ComplexNumber operator* (ComplexNumber); 
     ComplexNumber operator/ (ComplexNumber); 
     ComplexNumber operator= (ComplexNumber); 
     bool operator== (ComplexNumber); 
     friend ostream& operator<< (ostream&, ComplexNumber); 
}; 

void Menu(); 

Proj_11.cpp

#include "Proj_11.h" 

//Global Constants 
const double NOTHING = 0.0; 
const double INVERSE = -1.0; 
const int CURRENCY_FORMAT = 2; 

void main() 
{ 
    Menu(); 
} 

void Menu() 
{ 
    // Create complex numbers to do arithmentic with 
    ComplexNumber cm1(1, 2); 
    ComplexNumber cm2(1, -2); 

    // test addition operator 
    ComplexNumber cm3 = cm1 + cm2; 
    cout << cm3 << endl; 

    // test subtraction operator 
    ComplexNumber cm4 = cm1 - cm2; 
    cout << cm4 << endl; 

    // test multiplication operator 
    ComplexNumber cm5 = cm1 * cm2; 
    cout << cm5 << endl; 

    // test division operator 
    ComplexNumber cm6 = cm1/cm2; 
    cout << cm6<< endl; 

    // test assignment operator 
    cm6 = cm5; 
    cout << cm6 << endl; 

    // test comparison operator 
    if (cm1 == cm2) 
     cout << "\nThey are equal.\n"; 
    else 
     cout << "\nThey are not equal."; 

    ComplexNumber cm8(1, 2); 
    if (cm1 == cm8) 
     cout << "\nThey are equal.\n"; 
    else 
     cout << "\nThey are not equal."; 

    system ("PAUSE"); 
} 

ComplexNumber::ComplexNumber() 
{ 
    _real = 0.0; 
    _imaginary = 0.0; 
} 

ComplexNumber::ComplexNumber(double initReal, double initImaginary) 
{ 
    _real = initReal; 
    _imaginary = initImaginary; 
} 

ComplexNumber::ComplexNumber(ComplexNumber& cmplx) 
{ 
    _imaginary = cmplx._imaginary; 
    _real = cmplx._real; 
} 

ComplexNumber ComplexNumber::operator+ (ComplexNumber x) 
{ 
    double newReal = _real + x._real; 
    double newImaginary = _imaginary + x._imaginary; 
    ComplexNumber temp(newReal, newImaginary); 
    return temp; 
} 

ComplexNumber ComplexNumber::operator- (ComplexNumber x) 
{ 
    double newReal = _real - x._real; 
    double newImaginary = _imaginary - x._imaginary; 
    ComplexNumber temp(newReal, newImaginary); 
    return temp; 
} 

ComplexNumber ComplexNumber::operator* (ComplexNumber x) 
{ 
    double newReal = 0.0; 
    double newImaginary = 0.0; 
    //(a+b)*(c+d) = ac+bc+ad+bd 
    newReal = newReal + (_real * x._real); 
    newImaginary = newImaginary + (_imaginary * x._real); 
    newImaginary = newImaginary + (_real * x._imaginary); 
    newReal = newReal + (INVERSE * (_imaginary * x._imaginary)); 
    ComplexNumber temp(newReal, newImaginary); 
    return temp; 
} 

ComplexNumber ComplexNumber::operator/ (ComplexNumber x) 
{ 
    double newReal = 0.0; 
    double newImaginary = 0.0; 
    ComplexNumber conjugate(x._real, (INVERSE * x._imaginary)); 
    ComplexNumber numerator = (*this * conjugate); 
    ComplexNumber denominator = (x * conjugate); 
    newReal = numerator._real/denominator._real; 
    newImaginary = numerator._imaginary/denominator._real; 
    ComplexNumber temp(newReal, newImaginary); 
    return temp; 
} 

ComplexNumber ComplexNumber::operator= (ComplexNumber x) 
{ 
    ComplexNumber temp(x._real, x._imaginary); 
    return temp; 
} 

bool ComplexNumber::operator== (ComplexNumber x) 
{ 
    if ((_real == x._real) && (_imaginary == x._imaginary)) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

ostream& operator<< (ostream& out, ComplexNumber x) 
{ 
    out.setf(ios::fixed); 
    out.precision(CURRENCY_FORMAT); 
    if ((x._real != NOTHING) && (x._imaginary != NOTHING)) 
    { 
     if ((x._real > NOTHING) && (x._imaginary > NOTHING)) 
     { 
      out << x._real << " + " << x._imaginary << "i"; 
      return out; 
     } 
     else if ((x._real > NOTHING) && (x._imaginary < NOTHING)) 
     { 
      out << x._real << " - " << (INVERSE * x._imaginary) << "i"; 
      return out; 
     } 
     else if ((x._real < NOTHING) && (x._imaginary > NOTHING)) 
     { 
      out << x._real << " + " << x._imaginary << "i"; 
      return out; 
     } 
     else 
     { 
      out << x._real << " - " << (INVERSE * x._imaginary) << "i"; 
      return out; 
     } 
    } 
    else if ((x._real == NOTHING) && (x._imaginary != NOTHING)) 
    { 
     out << x._imaginary << "i"; 
     return out; 
    } 
    else if ((x._real != NOTHING) && (x._imaginary == NOTHING)) 
    { 
     out << x._real; 
     return out; 
    } 
    else 
    { 
     out << NOTHING; 
     return out; 
    } 
} 
+0

'main'必須返回'int'的。而且從不**在標題中放置使用指令。您還應該從該運算符重載中返回一個引用,並實際修改該對象。說到這些,其他的像'operator /'不會修改對象,所以它們應該被標記爲'const'。 – chris

+1

請得到一個體面的C++教科書。你的代碼中有一些令人頭痛的嚴重錯誤。 –

+0

Kerrek是對的,我不能再感覺到我的頭了。 –

回答

3

你的賦值運算符簽名的聲明應該是

ComplexNumber& operator= (const ComplexNumber&); 

,並返回一個引用當前實例:

ComplexNumber& operator= (const ComplexNumber& other) 
{ 
    // copy members from other ... 
    return *this; 
} 

...當中有很多你會遇到其他錯誤你碼。

+0

謝謝!看起來我有更多的重讀操作符,哈哈。 – user3117263

+0

@ user3117263很高興有幫助;)... –

1

複製構造函數應該採取const&而不是&

operator=應修改this狀態。

你的流是醜陋的,你不應該用這種方式搞砸精度。設置外部精度。它可能有其他問題。

一元-在許多方面比*INVERSE更好。

因各種原因擺脫NOTHING,其中包括0.0

1

你所做的就是你所得到的。您無法更改指定的對象。 而是這個

ComplexNumber ComplexNumber::operator= (ComplexNumber x) 
{ 
    ComplexNumber temp(x._real, x._imaginary); 
    return temp; 
} 

倒不如寫的拷貝賦值運算符通過以下方式

ComplexNumber & ComplexNumber::operator= (const ComplexNumber &x) 
{ 
    if (this != &x) 
    { 
     _real = x._real; 
     _imaginary = x._imaginary; 
    } 

    return (*this); 
}