weixin_31884495 2015-10-20 10:04 采纳率: 0%
浏览 1406

C++运算符重载问题友元函数

#include#includeusing namespace std;class Complex{ public: Complex(double newx=0.0, double newy=0.0 ); //Complex(); Complex(Complex &c); double getx() const; double gety() const; Complex Add(const Complex &another); void Output(); friend istream & operator>>(istream &in, const Complex &c); friend ostream & operator<<(ostream &out, const Complex &c); Complex operator+(const Complex &c2)const; ~Complex(){}; private: double x,y;};Complex::Complex(double newx, double newy){ x=newx; y=newy;}/*Complex::Complex(){ x=0; y=0;}*/Complex::Complex(Complex &c){ x=c.x; y=c.y;}double Complex::getx()const{ return x;}double Complex::gety()const{ return y;}Complex Complex::Add(const Complex &another){ Complex r; r.x=x+another.getx(); r.y=y+another.gety(); return r;}void Complex::Output(){ cout<=0) cout<<"+"; cout<>(istream &in, const Complex &c){// in>>"(">>c.x>>","<>c.x; in>>c.y; return in;}ostream & operator<<(ostream &out, const Complex &c){ out<<"("<>z1; cin>>z2; z4=z1.Add(z2); z4.Output(); z3 = z1+z2; z3.Output(); return 0;}

运行后错误: error C2248: 'x' : cannot access private member declared in class 'Complex'

see declaration of 'x'

error C2593: 'operator >>' is ambiguouserror C2248: 'y' : cannot access private member declared in class 'Complex'

反正就是不能访问私有成员什么的

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。