Please do read before flagging as a duplicate
I am overloading operators >> and << for reading complex numbers with real part r and imaginary part i;
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class complex
{
int r,i;
public:
complex()
{ i=r=0;}
friend istream& operator>>(istream&, complex&);
friend ostream& operator<<(ostream&,complex&);
};
istream& operator>>(ifstream &din, complex &x)
{
din>>x.r;
din>>x.i;
return din;
}
ostream& operator<<(ostream &dout, complex &x)
{
dout<<x.r<<x.i;
return dout;
}
void main()
{
clrscr();
complex x;
cin>>x;
cout<<x;
}
The error is that r and i are not accessible at code part
din>>x.r;
din>>x.i;
The error is that r and i are private so not accessible Aren't normal friend functions able to access private variables. Why does it fail for >> only?
Note: << operator works fine. only >> fails