【发布时间】:2018-11-15 08:39:25
【问题描述】:
int main()
{
CComplex c1(9,9);
CComplex c3;
c3 = 5 + c1; // getting error here
c3.print();
cout << c3;
return 0;
}
CComplex CComplex::operator+(const CComplex &complex)
{
CComplex temp;
temp.m_real = complex.m_real + m_real;
temp.m_imaginary = complex.m_imaginary + m_imaginary;
return temp;
}
【问题讨论】:
-
不确定这是否是骗局:What are the basic rules and idioms for operator overloading?(似乎没有一个答案能解决您需要 2 个运算符重载的问题)
-
您需要为 int 重载 + 运算符,而不是复杂的。像这样:
operator+(int c) -
为了将来参考,请说明并解释问题所在,您尝试了什么,并将错误完整附在问题中。
-
@itsundefined 这里需要一个非成员重载
operator+(int, CComplex)。 -
他的问题不清楚是否要支持两者
标签: c++ constructor operator-overloading