就像我们在 cin 中使用的那样:
cin >> a >> b;
从输入流中获取多个值并将它们插入到多个变量中。我们如何在自己的类中实现这个方法?从中获取多个值。我尝试了这个发现here:
#include <iostream>
using namespace std;
class example {
friend istream& operator>> (istream& is, example& exam);
private:
int my = 2;
int my2 = 4;
};
istream& operator>> (istream& is, example& exam) {
is >> exam.my >> exam.my2;
return is;
}
int main() {
example abc;
int s, t;
abc >> s >> t;
cout << s << t;
}
但是出现错误“与运算符不匹配>>(操作数类型为'example'和'int')”
PS:我知道其他方法,但我想知道这种具体的方法,谢谢。