主题:高手进!已经用了友元,为什么编译器总是报错,提示不能访问私有数据成员?
原程序如下:
#include <iostream>
using namespace std;
class COMPLEX
{
private:
float real, imag;
public:
COMPLEX()
{
real = 0;
imag = 0;
}
COMPLEX(float a, float b)
{
real = a;
imag = b;
}
~COMPLEX(){}
friend ostream& operator<< (ostream& putout, COMPLEX& obj);
friend istream& operator>> (istream& getin, COMPLEX& obj);
COMPLEX operator+ (COMPLEX &x);
COMPLEX operator- (COMPLEX &x);
COMPLEX operator* (COMPLEX &x);
COMPLEX operator/ (COMPLEX &x);
};
ostream& operator<< (ostream& putout,const COMPLEX& obj)
{
putout << obj.real;
if (obj.imag > 0) putout << "+";
if (obj.imag != 0) putout << obj.imag << "i";
return putout;
}
istream& operator>> (istream& getin, COMPLEX& obj)
{
char ch, i;
cout << " Input the complex: ";
getin >> obj.real >> ch;
getin >> obj.imag;
if (ch == '-') obj.imag = -obj.imag;
getin >> i;
if (i != 'i')
{
switch (ch)
{
case '+' : obj.real += obj.imag; obj.imag = 0; break;
case '-' : obj.real += obj.imag; obj.imag = 0; break;
case '*' : obj.real *= obj.imag; obj.imag = 0; break;
case '/' : obj.real /= obj.imag; obj.imag = 0; break;
default : cout << "Error complex!";
}
}
return getin;
}
COMPLEX COMPLEX::operator+ (COMPLEX &x)
{
return COMPLEX(real + x.real, imag + x.imag);
}
COMPLEX COMPLEX::operator- (COMPLEX &x)
{
return COMPLEX(real - x.real, imag - x.imag);
}
COMPLEX COMPLEX::operator* (COMPLEX &x)
{
return COMPLEX( (real * x.real) - (imag * x.imag),
(real * x.imag) + (imag * x.real) );
}
COMPLEX COMPLEX::operator/ (COMPLEX &x)
{
return COMPLEX( (real * x.real + imag * x.imag) *
( 1 / (x.real * x.real + x.imag *x.imag) ) , (x.real * imag - real * x.imag) *
( 1 / (x.real * x.real + x.imag *x.imag) ));
}
int main()
{
COMPLEX j(1,2), k(4,-5), m, n;
cout << j << endl;
cout << k << endl;
cout << j + k << endl;
cout << j - k << endl;
cout << j * k << endl;
cout << j / k << endl;
cin >> m >> n;
cout << m << endl;
cout << n << endl;
cout << m + n << endl;
cout << m - n << endl;
cout << m * n << endl;
cout << m / n << endl;
cin.get();
return (0);
}
#include <iostream>
using namespace std;
class COMPLEX
{
private:
float real, imag;
public:
COMPLEX()
{
real = 0;
imag = 0;
}
COMPLEX(float a, float b)
{
real = a;
imag = b;
}
~COMPLEX(){}
friend ostream& operator<< (ostream& putout, COMPLEX& obj);
friend istream& operator>> (istream& getin, COMPLEX& obj);
COMPLEX operator+ (COMPLEX &x);
COMPLEX operator- (COMPLEX &x);
COMPLEX operator* (COMPLEX &x);
COMPLEX operator/ (COMPLEX &x);
};
ostream& operator<< (ostream& putout,const COMPLEX& obj)
{
putout << obj.real;
if (obj.imag > 0) putout << "+";
if (obj.imag != 0) putout << obj.imag << "i";
return putout;
}
istream& operator>> (istream& getin, COMPLEX& obj)
{
char ch, i;
cout << " Input the complex: ";
getin >> obj.real >> ch;
getin >> obj.imag;
if (ch == '-') obj.imag = -obj.imag;
getin >> i;
if (i != 'i')
{
switch (ch)
{
case '+' : obj.real += obj.imag; obj.imag = 0; break;
case '-' : obj.real += obj.imag; obj.imag = 0; break;
case '*' : obj.real *= obj.imag; obj.imag = 0; break;
case '/' : obj.real /= obj.imag; obj.imag = 0; break;
default : cout << "Error complex!";
}
}
return getin;
}
COMPLEX COMPLEX::operator+ (COMPLEX &x)
{
return COMPLEX(real + x.real, imag + x.imag);
}
COMPLEX COMPLEX::operator- (COMPLEX &x)
{
return COMPLEX(real - x.real, imag - x.imag);
}
COMPLEX COMPLEX::operator* (COMPLEX &x)
{
return COMPLEX( (real * x.real) - (imag * x.imag),
(real * x.imag) + (imag * x.real) );
}
COMPLEX COMPLEX::operator/ (COMPLEX &x)
{
return COMPLEX( (real * x.real + imag * x.imag) *
( 1 / (x.real * x.real + x.imag *x.imag) ) , (x.real * imag - real * x.imag) *
( 1 / (x.real * x.real + x.imag *x.imag) ));
}
int main()
{
COMPLEX j(1,2), k(4,-5), m, n;
cout << j << endl;
cout << k << endl;
cout << j + k << endl;
cout << j - k << endl;
cout << j * k << endl;
cout << j / k << endl;
cin >> m >> n;
cout << m << endl;
cout << n << endl;
cout << m + n << endl;
cout << m - n << endl;
cout << m * n << endl;
cout << m / n << endl;
cin.get();
return (0);
}