大佬们为什么我那样改,下面+符号会提示出错
}
在学习重载这章,为啥加了取地址符就不对了,如图。但是如果加法两边都是类对象的话,加不加取地址都对。大佬们解释下,为什么啊,啥时候要加取地址符,啥时候不用呢```
#include<iostream>
using namespace std;
class complex
{
public:
complex(){ real = 0; image = 0; }
complex(double r){ real = r; image = 0; }
complex(double r, double i) :real(r), image(i){}
friend ostream&operator<<(ostream&, complex&);
friend complex operator+(complex c1, complex c2); //(complex &c1, complex &c2)
private:
double real, image;
};
complex operator+(complex c1, complex c2) //改成这个出错,为什么(complex &c1, complex &c2)
{
complex c;
c.real = c1.real + c2.real;
c.image = c1.image + c2.image;
return(c);
}
ostream&operator<<(ostream&output, complex &c2)
{
return(output << "(" << c2.real << "+" << c2.image << "i)" << endl);
}
int main()
{
complex c1(5, 4), c2(5, -10), c3;
c3 = c1 + c2;
cout << c3 ;
while (1);