gpt4 book ai didi

c++ - C++ 运算符重载错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:05 25 4
gpt4 key购买 nike

#include<iostream>
using namespace std;

class complex {
double real;
double image;
public:
complex(double r=0,double i=0) : real(r), image(i) { };
complex(const complex& c) : real(c.real), image(c.image) { };
~complex(){};
double re() const {
return real;
};
double im() const{
return image;
};
const complex& operator =(const complex&c)
{
real = c.real;
image = c.image;
return *this;
};
const complex& operator +=(const complex&c)
{
real += c.real;
image += c.image;
return *this;
};
const complex& operator -=(const complex&c)
{
real -= c.real;
image -= c.image;
return *this;
};
const complex& operator *=(const complex&c)
{
double keepreal=real;
real = real * c.real - image * c.image;
image = keepreal * c.image + image * c.real;
return *this;
};
const complex& operator /=(double d)
{
real/=d;
image/=d;
return *this;
};

friend complex operator !(const complex &c)
{
return complex(c.re(),-c.im());
};
friend double abs2(const complex& c)
{
return (c.re() * c.re() + c.im() * c.im());
};
const complex& operator /=(const complex&c)
{
return *this *= (!c) /= abs2(c);
};
const complex operator +(const complex& c, const complex& d)
{
return complex(c.re() + d.re(), c.im() + d.im());
};
const complex operator -(const complex& c, const complex& d)
{
return complex(c.re() - d.re(), c.im() - d.im());
};
const complex operator -(const complex&c)
{
return complex(-c.re(), -c.im());
};
const complex operator /(const complex& c,const complex& d)
{
return complex(c) /= d;
};
};

int main() {
complex c = 1., d(3.,4.);

return 0;
}

输出:

Line 62: error: 'const complex complex::operator+(const complex&, const complex&)' must take either zero or one argument
compilation terminated due to -Wfatal-errors.

请帮忙:http://codepad.org/cOOMmqw1

最佳答案

有两种方法可以重载二元 + 运算符,作为自由函数或作为成员函数。作为成员函数,签名是 Type operator+( Type const & ) const (const 在那里是可选的,但我假设 a+b不修改 a,这似乎是一个合理的假设)。

另一种方法是使用一个自由函数,它接受两个对象并返回总和。对此有不同的签名,但最广泛接受的是:Type operator+(Type lhs, Type const & rhs)(注意第一个参数是按值) ,其中实现在内部修改并返回 lhs。特别是算术运算符重载的一种常见方法是将 operator+= 实现为成员函数,然后根据前者实现一个自由​​函数 operator+:

struct Type {
// ...
Type& operator+=( Type const & );
};
Type operator+( Type lhs, Type const & rhs ) {
return lhs+=rhs;
}

这样,您就不需要将友元授予 free 函数。在某些情况下,特别是模板,有时建议在类定义中定义运算符,在这种情况下,您必须将其设为 friend(不是为了访问,而是出于句法原因:

struct Type {
// ...
Type& operator+=( Type const & );
// Still a free function:
friend Type operator+( Type lhs, Type const & rhs ) {
return lhs+=rhs;
}
};

至于使用这种模式的原因...首先实现 operator+= 然后在其之上实现 operator+ 提供了两个独立的操作,而额外的成本很小(operator+ 是一个单行函数!)。将其实现为成员函数(operator+= 也可以是自由函数)使其类似于 operator=(必须是成员)并且避免了友元的需要。

operator+实现为自由函数的原因与操作的类型对称性有关。加法是可交换的 (a+b == b+a),您在使用您的类型时会期望相同。您提供了一个隐式构造函数(由于构造函数 complex( double r = 0, double i = 0 ),您的复杂类型可以从 int 隐式转换,并且允许编译器使用这些转换如果函数调用不完全匹配重载。

如果 operator+ 被实现为一个成员函数,编译器只允许在第一个参数是一个复数时考虑重载,并且会隐式转换另一个参数,允许您键入 complex(0,0)+5。问题是,如果您反转 5+complex(0,0) 的顺序,编译器无法将 5 转换为复数,然后使用成员 operator+。另一方面,如果您将其作为自由函数提供,编译器将检测到两个参数中的一个在两种情况下都匹配,并将尝试并成功转换另一个参数。最终结果是,通过使用一个自由函数,您可以通过一个实现实现所有这三个添加:complex+complexcomplex+doubledouble+complex (另外对于所有整数和浮点类型,因为它们可以转换为 double

operator+ 按值获取第一个参数意味着编译器可以在某些情况下省略拷贝:a + b + c 绑定(bind)为 (a+ b) + c,第一次操作的临时结果可以直接作为第二次调用的参数,不需要额外的复制。

关于c++ - C++ 运算符重载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com