重载分配运算符的问题

时间:2013-12-19 00:02:01

标签: c++ operator-overloading

所以我遇到的问题是在我的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;
    }
}

3 个答案:

答案 0 :(得分:3)

您对赋值运算符签名的声明应为

 ComplexNumber& operator= (const ComplexNumber&);

并返回对当前实例的引用:

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

...在您的代码中会遇到很多其他错误。

答案 1 :(得分:1)

复制构造函数应该是const&而不是&

operator=应修改this州。

你的流出来是丑陋的,你不应该那么精确。在外面设定精度。它可能还有其他问题。

一元-在许多方面优于*INVERSE

出于各种原因摆脱NOTHING,包括0.0

答案 2 :(得分: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 );
}