У меня есть следующий фрагмент кода:
class Student {
public:
Student(){}
void display() const{}
friend istream& operator>>(istream& is, Student& s){return is;}
friend ostream& operator<<(ostream& os, const Student& s){return os; }
};
int main()
{
Student st;
cin >> st;
cout << st;
getch();
return 0;
}
Я пробовал себя, опуская ключевые слова friend
, чтобы операторы стали функцией-членом класса Student, тогда компилятор выдаст "binary 'operator >>' has too many parameters
". Я читал документ, в котором говорилось, что это произошло потому, что все функции-члены всегда получают неявный параметр «this» (поэтому все функции-члены могут получать доступ к закрытым переменным).
Исходя из этого объяснения, я попытался сделать следующее:
class Student {
public:
Student(){}
void display() const{}
istream& operator>>(istream& is){return is;}
ostream& operator<<(ostream& os){return os; }
};
int main()
{
Student st;
cin >> st;
cout << st;
getch();
return 0;
}
И получил сообщение об ошибке: "error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Student' (or there is no acceptable conversion)
"
Может кто-нибудь дать мне четкое объяснение, пожалуйста?