私は次のコードがありますC++のカスタムI/O演算子に関する質問?
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;
}
をオペレーターがStudentクラスのメンバ関数となり、その後、コンパイラは「binary 'operator >>' has too many parameters
」を生み出す作るためにfriend
キーワードを省略したとき、私は自分自身を試してみました。私は、すべてのメンバ関数が常に暗黙のパラメータ "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)
」
を誰もが、私に明確な説明をしてください与えることはできますか?