2013-12-19 7 views
1

그래서 오버로드 된 할당 연산자를 테스트하려고하면 C++ 프로그램에서 문제가 발생합니다.오버로드 할당 연산자 문제

문 :

cm6 = cm5; 

은 CM5와 같다 CM6 설정해야하지만, 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'의 . 그리고 ** 헤더에 using 지시어를 사용하지 마십시오. 또한 해당 연산자 오버로드에서 참조를 반환하고 실제로 개체를 수정해야합니다. 말하자면'operator /'와 같은 다른 것들은 객체를 수정하지 않기 때문에'const'라고 표시해야합니다. – chris

+1

괜찮은 C++ 교과서를 구하십시오. 코드 전체에 심각한 실수가 수없이 많습니다. –

+0

Kerrek이 옳다. 나는 더 이상 내 머리를 느낄 수 없다. –

답변

3

할당 연산자 서명 귀하의 선언은

ComplexNumber& operator= (const ComplexNumber&); 

하고 현재 인스턴스에 대한 참조를 반환해야합니다 : 당신이 직면하게 될 다른 많은 오류 사이에 ...

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

당신의 암호.

+0

고마워! Overloading Operators에서 많은 것을 읽은 것으로 보입니다. – user3117263

+0

@ user3117263 도움이 된다니 기쁘다;) ... –

1

복사 생성자는 이 아닌 &이어야합니다.

operator=this 상태를 수정해야합니다.

스트림이 엉망이며, 그런 식으로 정밀도가 떨어지지 않아야합니다. 외부 정밀도를 설정하십시오. 아마도 다른 문제가있을 것입니다.

단 하나 -는 여러 가지면에서 *INVERSE보다 좋습니다.

0.0을 포함하여 모든 이유에 대해 NOTHING을 제거하십시오.

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); 
}