重写 C++ 中的运算符重载?
大家好,
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如何放入以下形式的构造函数:
以便任何可以转换为
float
的值都可以作为Complex
被摄取。然后,您不需要为各种类型创建重载运算符。您为Complex
编写的那个就足够了。How about putting in a constructor of the form:
so that any value which can be cast to a
float
can be ingested as aComplex
. Then, you don't need to make an overloaded operator for all sorts of types. The one you wrote forComplex
will suffice.您收到不明确的重载错误的原因是您有
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 twoComplex
or aComplex
and adouble
, but you're trying to add aComplex
and anint
. The compiler can't decide if its better to convert theint
toComplex
to use the first or todouble
and use the second.To avoid this, you need to either define overloaded
operator+
for all possible types you might want to add to aComplex
(int, float, long, unsigned...) OR not overloadoperator+
in the first place -- just define a SINGLEoperator+
that adds twoComplex
and let type conversions deal with all the other cases.为双操作数重载
operator+
:Overload
operator+
for a double operand:如果您打算这样做,您可能需要执行以下操作。首先,我们定义以下内容(在类外部,以便允许第一个和第二个操作数进行隐式转换),不要定义任何其他运算符+,例如operator+(Complex,double):
同时定义一个隐式构造函数:
然后定义转换运算符(就像wheaties指出的那样,这可以被认为是一种不好的编程习惯,我同意这一点;所以如果不需要最终转换为double,则省略此操作):
这将自动支持
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):
At the same time define an implicit constructor:
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):
This will automatically support
double c = a_complex + 10;
anddouble c = 10 + a_complex;
, the number10
will implicitly converted into aComplex
using the implicit constructor and the arithmetic will resolve tooperator+(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 implementoperator+
above.