#include <iostream>
#include <cmath>
using namespace std;
class complex
{
public:
complex(int a,int b):real(a),imag(b){}
complex() { real = 0; imag = 0; }
friend istream& operator>>(istream&,complex&);
friend ostream& operator<<(ostream&, const complex&);
complex operator++();
complex operator++(int);
friend complex operator+(complex&, complex&);
complex(int a) { real = a; imag = 0; }
private:
int real;
int imag;
};
istream& operator>>(istream& input,complex& c)
{
cout << "enter the real and imag" << endl;
input >> c.real >> c.imag;
return input;
}
complex operator +(complex& c1, complex& c2)
{
return complex(c1.real + c2.real, c1.imag + c2.imag);
}
ostream& operator<<(ostream& output, const complex& c)
{
if (c.imag < 0)
output << c.real << "-" << abs(c.imag) << "i" << endl;
else if (c.imag == 0)
output << c.real << endl;
else
output << c.real << "+" << c.imag << "i" << endl;
return output;
}
complex complex::operator++()
{
complex temp(this->real+1,this->imag+1);
return temp;
}
complex complex::operator++(int)
{
complex temp(*this);
*this = complex(this->real + 1, this->imag + 1);
return temp;
}
int main()
{
complex c1(1, 2), c2(3, -1), c4, c5,c6,c8;
cout << c1 << '\t' << c2 << endl;
c4 = c1++;
cout << c4 << endl;
c5 = ++c2;
int i = 3;
cout << c5 << endl;
cout << c1 << endl;
cout << complex(i) << endl;
c8 = complex(i);
c6 =complex(i)+c2;
cout << c6;
complex c7;
cout << "enter c7" << endl;
cin >> c7;
cout << c7 << endl;
system("pause");
return 0;
}
错误(活动) E0349 没有与这些操作数匹配的 "+" 运算符
错误 C2678 二进制“+”: 没有找到接受“complex”类型的左操作数的运算符(或没有可接受的转换)