[問題] operator overloading的問題

作者: peggypiano (大N)   2016-10-02 19:57:12
已解決...
發現得是
a.apple=b[0];
a.pen=b[1];
a.pinapple=b[2];
抱歉犯蠢 囧
弄了很久都沒發現 囧
問題(Question):
不知道為什麼無法存取到新給的input
餵入的資料(Input):
1 2 3
預期的正確結果(Expected Output):
1
錯誤結果(Wrong Output):
2
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
using namespace std;
class Poly
{
public:
int apple;
int pen;
int pinapple;
friend istream& operator >>(istream& ins, Poly& a);
};
istream& operator >>(istream& ins, Poly& a)
{
int b[3];
ins >> b[0] >> b[1] >> b[2];
b[0]=a.apple;
b[1]=a.pen;
b[2]=a.pinapple;
return ins;
}
int main()
{
Poly a;
a.apple=2;
cout << a.apple;
cin >> a;
cout << a.apple;
return 0;
}
補充說明(Supplement):
我希望可以cin 一個陣列然後 把值存進去 a,
但不知道問題出在哪裡QQ
先謝謝幫看的大大
作者: Frozenmouse (*冰之鼠*)   2016-10-02 20:05:00
operator>>的實作給值的地方寫反了?

Links booklink

Contact Us: admin [ a t ] ucptt.com