VS2010C++为什么会出现这么多错误 程序如下。友元重载运算符。
#include<iostream>usingnamespacestd;classcomplex{public:complex(){real=imag=0;}comple...
#include <iostream>
using namespace std;
class complex
{public:
complex()
{real=imag=0;}
complex(double i,double r)
{real=i;
imag=r;}
friend complex operate +(const complex&c1,const complex&c2);
friend complex operate -(const complex&c1,const complex&c2);
friend complex operate *(const complex&c1,const complex&c2);
friend complex operate /(const complex&c1,const complex&c2);
void print(const complex&c)
{if(c.imag<0)
cout<<c.real<<c.imag<<'i'<<endl;
else
cout<<c.real<<'+'<<c.imag<<'i'<<endl;}
private:
double real,imag;
};
complex operate +(const complex&c1,const complex&c2)
{return complex(c1.real+c2.real,c1.imag+c2.imag);}
complex operate -(const complex&c1,const complex&c2)
{return complex(c1.real-c2.real,c1.imag-c2.imag);}
complex operate *(const complex&c1,const complex&c2)
{return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real)
complex operate /(const complex&c1,const complex&c2)
{return complex(c1.real*c2.real+c1.imag*c2.imag/c2.real*c2.real+c2.imag*c2.imag,c1.imag*c2.real-c1.real*c2.imag/c2.real*c2.real+c2.imag*c2.imag);}
int main()
{complex c1(2.0,3.0),c2(4.0,-2.0),c3;
c3=c1+c2;
cout<<"\nc1+c2=";
print(c3);
c3=c1-c2;
cout<<"\nc1-c2";
print(c3);
c3=c1*c2;
cout<<"\nc1*c2";
print(c3);
c3=c1/c2;
cout<<"\nc1/c2";
print(c3);
c3=(c1+c2)*(c1-c2)*c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
return 0;
}
比如:C2433: “operate”: 不允许在数据声明中使用“friend”
求解谢谢谢谢谢!!!! 展开
using namespace std;
class complex
{public:
complex()
{real=imag=0;}
complex(double i,double r)
{real=i;
imag=r;}
friend complex operate +(const complex&c1,const complex&c2);
friend complex operate -(const complex&c1,const complex&c2);
friend complex operate *(const complex&c1,const complex&c2);
friend complex operate /(const complex&c1,const complex&c2);
void print(const complex&c)
{if(c.imag<0)
cout<<c.real<<c.imag<<'i'<<endl;
else
cout<<c.real<<'+'<<c.imag<<'i'<<endl;}
private:
double real,imag;
};
complex operate +(const complex&c1,const complex&c2)
{return complex(c1.real+c2.real,c1.imag+c2.imag);}
complex operate -(const complex&c1,const complex&c2)
{return complex(c1.real-c2.real,c1.imag-c2.imag);}
complex operate *(const complex&c1,const complex&c2)
{return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real)
complex operate /(const complex&c1,const complex&c2)
{return complex(c1.real*c2.real+c1.imag*c2.imag/c2.real*c2.real+c2.imag*c2.imag,c1.imag*c2.real-c1.real*c2.imag/c2.real*c2.real+c2.imag*c2.imag);}
int main()
{complex c1(2.0,3.0),c2(4.0,-2.0),c3;
c3=c1+c2;
cout<<"\nc1+c2=";
print(c3);
c3=c1-c2;
cout<<"\nc1-c2";
print(c3);
c3=c1*c2;
cout<<"\nc1*c2";
print(c3);
c3=c1/c2;
cout<<"\nc1/c2";
print(c3);
c3=(c1+c2)*(c1-c2)*c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
return 0;
}
比如:C2433: “operate”: 不允许在数据声明中使用“friend”
求解谢谢谢谢谢!!!! 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询