就像我們在CIN使用:如何使用提取操作符從C++中的類的方法中獲取多個值?
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;
}
但得到錯誤「不匹配的操作>>(操作數類型是‘例如’和‘廉政’)」
PS:我知道的替代方式,但我想知道這樣做的具體方式,謝謝。
非常感謝。我明白這在編程方面不是一個好主意,但我只是有一種渴望知道它並且從一小時開始就在尋找。 –