[問題] operator overloading的問題

看板C_and_CPP (C/C++)作者 (大N)時間9年前 (2016/10/02 19:57), 9年前編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
已解決... 發現得是 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 先謝謝幫看的大大 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.136.219 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1475409437.A.E49.html ※ 編輯: peggypiano (140.113.136.219), 10/02/2016 20:04:15

10/02 20:05, , 1F
operator>>的實作給值的地方寫反了?
10/02 20:05, 1F
文章代碼(AID): #1NyFOTv9 (C_and_CPP)
文章代碼(AID): #1NyFOTv9 (C_and_CPP)