//重载流插入运算符和流提取运算符
//用重载的《进行输出
#include<iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;image=0;}
Complex(int i,int j){real=i;image=j;}
Complex operator +(Complex&c1);
friend ostream& operator<<(ostream&,Complex&c);
void display(){cout<<real<<", "<<image<<endl;
private:
int real;
int image;
}
Complex Complex::operator +(Complex&c1)
{
return Complex(real+c1.real,c1.image+image);
}
ostream& operator << (ostream&output,Complex&c)
{
output<<"( "<<c.real<<" + "<<c.image<<"i)"<<endl;
return output;
}
int main()
{
Complex c2(2,4),c3(3,6),c4;
c4=c2+c3;
cout<<c4;
return 0;
}
--------------------Configuration: chapter10 - Win32 Debug--------------------
Compiling...
pr4.cpp
E:\temp\chapter10\pr4.cpp(19) : error C2535: 'class Complex __thiscall Complex::operator +(class Complex &)' : member function already defined or declared
E:\temp\chapter10\pr4.cpp(11) : see declaration of '+'
E:\temp\chapter10\pr4.cpp(23) : error C2804: binary 'operator <<' has too many parameters
E:\temp\chapter10\pr4.cpp(23) : error C2333: '<<' : error in function declaration; skipping function body
E:\temp\chapter10\pr4.cpp(35) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
chapter10.exe - 4 error(s), 0 warning(s)
本人刚学C++,多多指教了