主题:求助朋友帮忙看下这个程序
请朋友帮忙看下这个程序
为什么将complex complex::operator+=(complex &p)和complex complex::operator-=(complex &p)改成注释那样的写法就错了呢?
头文件
//定义复数类,实现复数的+、-、+=、++先、++后、--先、--后运算符重载:成员函数和友元函数
#include<iostream>
using namespace std;
class complex
{
private:
double real,item;
public:
complex(double x,double y);
complex(complex &p);
void print();
complex operator+=(complex &p);
complex operator-=(complex &p);
complex operator++();
complex operator--();
};
complex::complex(double x,double y)
{
real=x;
item=y;
}
complex::complex(complex &p)
{
real=p.real;
item=p.item;
}
void complex::print()
{
cout<<real<<"+"<<item<<"i"<<endl;
}
complex complex::operator+=(complex &p)
{
real=real+p.real;
item=item+p.item;
return *this;
//为什么上面的程序改成下面注释的写法就错了呢?
/*double r,i;
r=p.real+real;
i=item+p.item;
return complex(r,i);
*/
}
complex complex::operator-=(complex &p)
{
real=real-p.real;
item=item-p.item;
return *this;
//为什么上面的程序改成下面注释的写法就错了呢?
/*double r,i;
r=p.real-real;
i=item-p.item;
return complex(r,i);
*/
}
complex complex::operator ++()
{
real++;
item++;
return *this;
}
complex complex::operator --()
{
real--;
item--;
return *this;
}
实现文件
#include"1.h"
#include<iostream>
#include<cstdlib>
void main()
{
complex a(3,4),b(4,5);
a.operator+=(b);
a.print();
system("pause");
}
为什么将complex complex::operator+=(complex &p)和complex complex::operator-=(complex &p)改成注释那样的写法就错了呢?
头文件
//定义复数类,实现复数的+、-、+=、++先、++后、--先、--后运算符重载:成员函数和友元函数
#include<iostream>
using namespace std;
class complex
{
private:
double real,item;
public:
complex(double x,double y);
complex(complex &p);
void print();
complex operator+=(complex &p);
complex operator-=(complex &p);
complex operator++();
complex operator--();
};
complex::complex(double x,double y)
{
real=x;
item=y;
}
complex::complex(complex &p)
{
real=p.real;
item=p.item;
}
void complex::print()
{
cout<<real<<"+"<<item<<"i"<<endl;
}
complex complex::operator+=(complex &p)
{
real=real+p.real;
item=item+p.item;
return *this;
//为什么上面的程序改成下面注释的写法就错了呢?
/*double r,i;
r=p.real+real;
i=item+p.item;
return complex(r,i);
*/
}
complex complex::operator-=(complex &p)
{
real=real-p.real;
item=item-p.item;
return *this;
//为什么上面的程序改成下面注释的写法就错了呢?
/*double r,i;
r=p.real-real;
i=item-p.item;
return complex(r,i);
*/
}
complex complex::operator ++()
{
real++;
item++;
return *this;
}
complex complex::operator --()
{
real--;
item--;
return *this;
}
实现文件
#include"1.h"
#include<iostream>
#include<cstdlib>
void main()
{
complex a(3,4),b(4,5);
a.operator+=(b);
a.print();
system("pause");
}