回 帖 发 新 帖 刷新版面

主题:高手进!已经用了友元,为什么编译器总是报错,提示不能访问私有数据成员?

原程序如下:

#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);
}

回复列表 (共2个回复)

沙发

#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, const COMPLEX& obj);
       
         //原先 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)

/*这如果用const COMPLEX& obj 在声明部分也得使用const COMPLEX& obj
  否则就去掉const ,前面也不用const COMPLEX& obj 直接
  用COMPLEX& obj 即可*/                 

{
    putout << obj.real;
    if (obj.imag > 0) putout << "+";
    if (obj.imag != 0) putout << obj.imag << "i";
    
    return putout;
}

板凳

除了你说的
VC还要#include<fstream>
CB就不用了

我来回复

您尚未登录,请登录后再回复。点此登录或注册