重写 C++ 中的运算符重载?

发布于 2024-09-01 16:54:31 字数 797 浏览 3 评论 0原文

大家好,

我有一个名为 Complex 的类,

我做了像这样的运算符重载

Complex c = a + b; // where a and b are object of Complex class 

,基本上是运算符 +(Complex& that);

但我不知道怎么说,例如

double c = a + 10; //where a is object of Complex class but 10 is integer / double  

我确实定义了类型转换为双倍得到我的IDE说有太多操作数+并且它以某种方式抱怨无法“理解”+

它必须采用这种格式虽然 double c = a + 10;

谢谢,

错误消息是

Error: more than one operator "+" matches these operands: 
error C2666: 'Rational::operator +' : 3 overloads have similar conversions 

1> could be 'const Complex Complex::operator +(const Complex &)' 1> 
or 'double operator +(const Complex &,double)' 

编译器无法根据签名进行选择?是的,我确实在课堂外定义了它,因为我在课堂内定义了它,谢谢

helo guys

i have class call Complex

I did operator overloading like such

Complex c = a + b; // where a and b are object of Complex class 

which basically is operator+(Complex& that);

but I dont know how to say for example

double c = a + 10; //where a is object of Complex class but 10 is integer / double  

I did define typecasting for a to be double get my IDE says that there are too many operands + and it somehow complains for not being able to "understand" the +

it has to be in this format though double c = a + 10;

thanks

error message is

Error: more than one operator "+" matches these operands: 
error C2666: 'Rational::operator +' : 3 overloads have similar conversions 

1> could be 'const Complex Complex::operator +(const Complex &)' 1> 
or 'double operator +(const Complex &,double)' 

the compiler can not pick based on signature ? and yes I did define it outside the class because I had one defined inside the class thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故人爱我别走 2024-09-08 16:54:31
double operator+(const Complex &c, int x)
{
    //....
}
double operator+(const Complex &c, int x)
{
    //....
}
一身软味 2024-09-08 16:54:31

如何放入以下形式的构造函数:

 Complex(float _real) : m_Real( _real ), m_Imaginary(0){}

以便任何可以转换为 float 的值都可以作为 Complex 被摄取。然后,您不需要为各种类型创建重载运算符。您为 Complex 编写的那个就足够了。

How about putting in a constructor of the form:

 Complex(float _real) : m_Real( _real ), m_Imaginary(0){}

so that any value which can be cast to a float can be ingested as a Complex. Then, you don't need to make an overloaded operator for all sorts of types. The one you wrote for Complex will suffice.

錯遇了你 2024-09-08 16:54:31

您收到不明确的重载错误的原因是您有 operator+ 变体,可以添加两个 Complex 或一个 Complex 和一个 double,但您尝试添加一个 Complex 和一个 int。编译器无法决定是将 int 转换为 Complex 以使用第一个更好,还是将 double 转换为 double 并使用第二个更好。

为了避免这种情况,您需要为您可能想要添加到 Complex 的所有可能类型定义重载的 operator+ (int、float、long、unsigned...)或者首先不要重载 operator+ - 只需定义一个单个 operator+ 来添加两个 Complex 并让类型转换处理所有其他情况。

The reason you're getting the ambiguous overload error is that you have operator+ variants that can add two Complex or a Complex and a double, but you're trying to add a Complex and an int. The compiler can't decide if its better to convert the int to Complex to use the first or to double and use the second.

To avoid this, you need to either define overloaded operator+ for all possible types you might want to add to a Complex (int, float, long, unsigned...) OR not overload operator+ in the first place -- just define a SINGLE operator+ that adds two Complex and let type conversions deal with all the other cases.

一影成城 2024-09-08 16:54:31

为双操作数重载 operator+

double Complex::operator+(double rhs)

Overload operator+ for a double operand:

double Complex::operator+(double rhs)
孤独患者 2024-09-08 16:54:31

如果您打算这样做,您可能需要执行以下操作。首先,我们定义以下内容(在类外部,以便允许第一个和第二个操作数进行隐式转换),不要定义任何其他运算符+,例如operator+(Complex,double):

Complex operator+(const Complex& a, const Complex& b) {
  // ..
}

同时定义一个隐式构造函数:

Complex(double a) : real(a), imag(0) {}

然后定义转换运算符(就像wheaties指出的那样,这可以被认为是一种不好的编程习惯,我同意这一点;所以如果不需要最终转换为double,则省略此操作):

operator double() const { return real; }

这将自动支持double c = a_complex + 10;double c = 10 + a_complex;,数字 10 将使用隐式转换为 Complex构造函数和算术运算将解析为 operator+(const Complex&, const Complex&);,结果将自动转换为 double。

PS您还可以定义Complex&类中的operator+=(const Complex& o) { /* .. */ } 并使用它来实现上面的operator+

If you plan to do this, you might want to do the following. First, we define the following (outside the class so that implicit conversion for both the first and second operand is allowed), do NOT define any other operator+, such as operator+(Complex,double):

Complex operator+(const Complex& a, const Complex& b) {
  // ..
}

At the same time define an implicit constructor:

Complex(double a) : real(a), imag(0) {}

And then define the conversion operator (like wheaties pointed out, this can be considered a bad programming practice, which I agree; so if the final conversion to double is not required, omit this):

operator double() const { return real; }

This will automatically support double c = a_complex + 10; and double c = 10 + a_complex;, the number 10 will implicitly converted into a Complex using the implicit constructor and the arithmetic will resolve to operator+(const Complex&, const Complex&);, the result will be converted to double automatically.

P.S. You may also define Complex& operator+=(const Complex& o) { /* .. */ } within the class and use this to implement operator+ above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文