大家帮忙看看这个C++小程序,3Q~~~
#include<iostream>usingnamespacestd;classcomplex{public:complex(){real=0;imag=0;}comp...
#include<iostream>
using namespace std;
class complex
{public:
complex(){real=0;imag=0;}
complex(double r){real=r;imag=0;}
complex(double r,double i){real=r;imag=i;}
complex(int i){real=i;imag=0;}
complex operator+(complex&,complex&);
void display();
private:
double real;
double imag;
};
complex complex::operator+(complex &c1,complex &c2)
{c1.real=c1.real+c2.real;
c1.imag=c1.imag+c2.imag;
return c1;
}
void complex::display()
{cout<<"("<<real<<","<<imag<<")"<<endl;}
int main()
{
complex c1(3,4),c2;
int i=5;
c2=i+c1;
cout<<"c1="
c1.display();
cout<<"c2="
c2.display();
system("PAUSE");
return EXIT_SUCCESS;
}
这个程序运行的时候complex operator+(complex&,complex&);这句为什么会报错呢 展开
using namespace std;
class complex
{public:
complex(){real=0;imag=0;}
complex(double r){real=r;imag=0;}
complex(double r,double i){real=r;imag=i;}
complex(int i){real=i;imag=0;}
complex operator+(complex&,complex&);
void display();
private:
double real;
double imag;
};
complex complex::operator+(complex &c1,complex &c2)
{c1.real=c1.real+c2.real;
c1.imag=c1.imag+c2.imag;
return c1;
}
void complex::display()
{cout<<"("<<real<<","<<imag<<")"<<endl;}
int main()
{
complex c1(3,4),c2;
int i=5;
c2=i+c1;
cout<<"c1="
c1.display();
cout<<"c2="
c2.display();
system("PAUSE");
return EXIT_SUCCESS;
}
这个程序运行的时候complex operator+(complex&,complex&);这句为什么会报错呢 展开
2个回答
展开全部
#include<iostream>
using namespace std;
////////////////////////////////////
class complex ;
const complex operator+(const complex&,const complex&);
///////////////////////////////////
class complex
{public:
complex(){real=0;imag=0;}
complex(double r){real=r;imag=0;}
complex(double r,double i){real=r;imag=i;}
complex(int i){real=i;imag=0;}
complex(complex &c){real=c.real;imag=c.imag;} //////////////////////
friend const complex operator+(const complex&,const complex&); //////////
void display();
private:
double real;
double imag;
};
const complex operator+(const complex &c1,const complex &c2) //////
{complex tmp; ///////////////////
tmp.real=c1.real+c2.real; ///////////////
tmp.imag=c1.imag+c2.imag; //////////////////
return tmp; ///////////////////////
}
void complex::display()
{cout<<"("<<real<<","<<imag<<")"<<endl;}
int main()
{
complex c1(3,4),c2;
int i=5;
c2=i+c1;
cout<<"c1=" ; ////////////////////
c1.display();
cout<<"c2=" ; ///////////////////
c2.display();
system("PAUSE");
return EXIT_SUCCESS;
}
using namespace std;
////////////////////////////////////
class complex ;
const complex operator+(const complex&,const complex&);
///////////////////////////////////
class complex
{public:
complex(){real=0;imag=0;}
complex(double r){real=r;imag=0;}
complex(double r,double i){real=r;imag=i;}
complex(int i){real=i;imag=0;}
complex(complex &c){real=c.real;imag=c.imag;} //////////////////////
friend const complex operator+(const complex&,const complex&); //////////
void display();
private:
double real;
double imag;
};
const complex operator+(const complex &c1,const complex &c2) //////
{complex tmp; ///////////////////
tmp.real=c1.real+c2.real; ///////////////
tmp.imag=c1.imag+c2.imag; //////////////////
return tmp; ///////////////////////
}
void complex::display()
{cout<<"("<<real<<","<<imag<<")"<<endl;}
int main()
{
complex c1(3,4),c2;
int i=5;
c2=i+c1;
cout<<"c1=" ; ////////////////////
c1.display();
cout<<"c2=" ; ///////////////////
c2.display();
system("PAUSE");
return EXIT_SUCCESS;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询