C++ 使用类添加复数

C++ 使用类添加复数,c++,operators,C++,Operators,我试图将两个复数相加,但得到了错误: 没有运算符“+”与这些操作数匹配 #include <iostream> using namespace std; class complex { public: double get_r() { return r; } void set_r(double newr) { r=newr; } double set_i() { return i; } void set_i(double newi) { i =

我试图将两个复数相加,但得到了错误:

没有运算符“+”与这些操作数匹配

#include <iostream>

using namespace std;


class complex
{
public:

    double get_r() { return r; }
    void set_r(double newr) { r=newr; }
    double set_i() { return i; }
    void set_i(double newi) { i = newi; }
private:
    double r, i;

};

int main()
{

complex A, B;
A.set_r(1.0);
A.set_i(2.0);
B.set_r(3.0);
B.set_i(2.0);

complex sum = A+B;
cout << "summen er: " << sum << endl;

        system("PAUSE");
return 0;
};

无运算符“您必须重载运算符+,运算符+是为内置类型和标准库中的某些类型定义的。由于
complex
是一个自定义类,因此您必须定义所有应对其进行操作的运算符

运算符+
可定义为:

class complex {
    ...
    complex operator + (const complex& other) {
        return complex(get_r() + other.get_r(), get_i() + other.get_i());
    }
    ...
};
注意,既不允许
A++
也不允许
A-B
。它们分别需要
complex&operator++()
complex operator-(const complex&)

对于流插入,第一个参数是流本身,因此必须定义一个friend运算符,该运算符在类之外有两个参数:

outstream& opererator << (outstream &out, const complex& val) {
    // output it the way you want
    return out;
}

<代码>外流和操作器> P>复数是C++标准的一部分。这里是从

中的例子。
#包括
#包括
#包括
#包括
int main()
{
使用名称空间std::complex_文本;

std::cout所有的算术运算符,如加号、减号、乘法或除法,只适用于预定义的数据类型,如int、char、float等

现在,如果你想在类中添加一些东西,你必须使用面向对象编程的基本方面,即操作符重载

以下是您如何实现它的方法

#include <iostream>
using namespace std;

class complex
{
    float x, y;
public:
    complex()
    {

    }

    complex(float real, float img)
    {
        x = real;
        y = img;
    }

friend complex operator+(complex,complex);
    void display(void);
};

complex operator+(complex c,complex d)
{
    complex t;
    t.x = d.x + c.x;
    t.y = d.y + t.y;
    return(t);
};

void complex::display(void)
{
    cout << x << "+i" << y << endl;
}




int main()
{
    complex c1, c2, c3;
    c1 = complex(2.5, 3.5);
    c2 = complex(1.5, 5.5);
    c3 = c1 + c2;//c3=opra+(c1,c2)
    cout << "C1:" << endl;
    c1.display();
    cout << "C2:" << endl;
    c2.display();
    cout << "C3:" << endl;
    c3.display();
}
#包括
使用名称空间std;
阶级情结
{
浮动x,y;
公众:
复合物()
{
}
复杂(浮点实数、浮点img)
{
x=真实值;
y=img;
}
友元复数运算符+(复数,复数);
作废显示(作废);
};
复数运算符+(复数c,复数d)
{
复t;
t、 x=d.x+c.x;
t、 y=d.y+t.y;
返回(t);
};
void complex::显示(void)
{

难道我找不到合适的位置来放置操作符+。因为我使用类,所以我需要一个操作符,对吗?基本操作符(+,++,,,这是你的类,编译器如何知道你想把它们加在一起?我不知道你在试图实现什么,但是也许你应该考虑使用STD::复杂;如果你想了解复杂的实现的更多内容,看看GNU ISO C++库是如何实现STD::复())我想我明白了。但是我不明白另一个。get_r()其他是什么?@AsgerBechNybo
A+B
将由编译器在
A.operator+(B)
中进行翻译。因此,这里,A将是
*这个
,而B是
其他
#include <iostream>
using namespace std;

class complex
{
    float x, y;
public:
    complex()
    {

    }

    complex(float real, float img)
    {
        x = real;
        y = img;
    }

friend complex operator+(complex,complex);
    void display(void);
};

complex operator+(complex c,complex d)
{
    complex t;
    t.x = d.x + c.x;
    t.y = d.y + t.y;
    return(t);
};

void complex::display(void)
{
    cout << x << "+i" << y << endl;
}




int main()
{
    complex c1, c2, c3;
    c1 = complex(2.5, 3.5);
    c2 = complex(1.5, 5.5);
    c3 = c1 + c2;//c3=opra+(c1,c2)
    cout << "C1:" << endl;
    c1.display();
    cout << "C2:" << endl;
    c2.display();
    cout << "C3:" << endl;
    c3.display();
}