2014-09-24 2 views
0

Я получаю странное уведомление о том, что я использую закрытые члены класса, что является полностью допустимым, но я, хотя мне это позволено, так как я сказал, что метод I Я использую дружественный.Частные члены класса - в этом контексте

Посмотрите на это:

#include <iostream> 

using namespace std; 


class complex { 

private: 
    double Re, Im; 

public: 

    complex(): Re(0.0), Im(0.0){} 
    complex(double Re, double Im): Re(Re), Im(Im){} 
    double getRe() const { return Re; } 
    double getIm() const { return Im; } 
    friend complex operator+(const complex&, const complex&); 
    friend ostream& operator<<(ostream&, const complex&); 
    friend istream& operator>>(istream &, const complex &); // FRIENDLY FUNCTION 
}; 


complex operator+(const complex& a, const complex& b) { 
    double r, i; 
    r = a.getRe()+ b.getRe(); 
    i = a.getIm() + b.getIm(); 
    return complex(r, i); 
} 

ostream& operator<<(ostream& out, const complex &a) { 
    out << "(" << a.getRe() << ", " << a.getIm() << ")" << endl; 
    return out; 
} 

istream &operator>>(istream &in, complex &c)  
{ 
    cout<<"enter real part:\n"; 
    in>>c.Re; // ** WITHIN THIS CONTEXT ERROR ** 
    cout<<"enter imag part: \n"; 
    in>>c.Im; // ** WITHIN THIS CONTEXT ERROR ** 
    return in; 
} 

int main(void) { 
complex a, b,c; 

cin >> a; 
cin >> b; 
c = a+b; 

cout << c; 

} 

Должен ли я объявить какую-то setFunction в классе, чтобы получить значения, которые являются частными?

ответ

5
istream& operator>>(istream &, const complex &); 

не то же самое, как

istream &operator>>(istream &in, complex &c); 

Пятно разница осталось как упражнение для читателя.

+0

был там сделано это. Все равно получайте точно такую ​​же ошибку :( –

Смежные вопросы