运算符重载类型complex / double的“+”

时间:2013-04-01 10:25:05

标签: c++

我只在Complex.cpp中添加了有用的信息。

这是我的问题:我使类复杂,这意味着它可以复杂计算。 在+运算符中我想启用complex + double,但我只能在main.cpp中使用complex + double。当我使用双变量+复数时,会出现错误。这是为什么?我能解决吗?

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H
using namespace std;
class Complex
{
public:
    Complex( double = 0.0, double = 0.0 ); // constructor
    Complex operator+( const Complex & ) const; // addition
    Complex operator-( const Complex & ) const; // subtraction
    Complex operator*( const Complex & ) const; // mul
    bool operator==( const Complex & ) const;
    bool operator!=( const Complex & ) const;
    friend ostream &operator<<( ostream & , const Complex& ); 
    friend istream &operator>>( istream & , Complex& );
    Complex operator+( const double & ) const;
    //Complex &operator+( const double & ) const;
    void print() const; // output
private:
    double real; // real part
    double imaginary; // imaginary part
}; // end class Complex

#endif

Complex.cpp

Complex Complex::operator+( const Complex &operand2 ) const
{
    return Complex( real + operand2.real,imaginary + operand2.imaginary );
} // end function operator+

Complex Complex::operator+(const double &operand2) const
{
    return Complex( real + operand2 , this->imaginary );
}

的main.cpp

int main()
{
Complex x;
Complex y( 4.3, 8.2 );
Complex z( 3.3, 1.1 );

    double ss = 5;
    x = z + ss;
    x = ss + z;//this syntax is illegal 

6 个答案:

答案 0 :(得分:3)

为了让您的班级显示为右手操作数,操作员需要是非会员。由于(在这种情况下)它需要访问私人成员,因此必须是朋友:

class Complex {
    // ...
    friend Complex operator+(double lhs, const Complex & rhs);
};

Complex operator+(double lhs, const Complex & rhs) {
    return Complex(lhs+rhs.real, rhs.imaginary);
}

或者,因为你已经有一个成员以相反的方式接受参数,并且加法是对称的,你可以定义一个非成员,非朋友的函数:

Complex operator+(double lhs, const Complex& rhs) {
    return rhs + lhs;
}

答案 1 :(得分:2)

operator+本质上是一个功能。 x = ss + z;x = ss.operator(z);类似,但ssdouble,内部未定义operator+。您需要定义一个类外operator+。 您可以在其他答案中建议的friend的帮助下完成此操作,但如果没有它可能会更清晰:

Complex operator+(const double& x, const Complex& c)
{
    return (c + x);
}

答案 2 :(得分:1)

在这种情况下你应该使用友元功能。

friend Complex operator+( const Complex&, const double & );
friend Complex operator+( const double&, const Complex&);
friend Complex operator+( const Complex&, const Complex&);

简单示例: http://liveworkspace.org/code/A14xS $ 0

答案 3 :(得分:0)

要实现此目的,您可以使用friend功能。将以下声明添加到您的班级

friend Complex operator+(const double&, const Complex&)

请注意,这不是该类的成员函数,因此实现应如下所示:

Complex operator+(const double& x, const Complex& c)
{
    return Complex(c.real + x, c.imaginary);
}

答案 4 :(得分:0)

定义运算符时,应始终提供完整集。对于operator+,这还包括operator+=,因为用户会认为如果cmpl = cmpl + 1.2有效,那么cmpl += 1.2也应该有效。您甚至可以使用Boost.Operators之类的帮助库,或者,如果允许使用C ++ 11,我的df.operators可以简化代码。对于operator+,您的代码将是:

#include <df/operators.hpp>

class Complex
  : df::commutative_addable< Complex >,
    df::commutative_addable< Complex, double >
{
public:
    Complex( double = 0.0, double = 0.0 ); // constructor
    Complex& operator+=( const Complex & ); // addition
    Complex& operator+=( const double & );

    // I skipped the rest...
};

会自动提供operator+的必要版本。

答案 5 :(得分:0)

定义成员函数operator+=(const Complex&),以及使用operator+(const Complex&, const Complex)进行数学运算的非成员函数operator+=。非会员功能不一定是朋友。这是这种事情的通常模式。