Re: [問題] 多載運算子>>

看板C_and_CPP (C/C++)作者 (Aider)時間14年前 (2012/01/13 16:24), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《thank1984 (把握當下)》之銘言: : 問題(Question>: : 各位大大 小弟宣告friend重載運算子>>後 在實做的部分 is>>t.real時編譯會出現問題 : 訊息為no match for 'operator>>'in 'is >> t->complex::real' 小弟不知道這樣的寫法 : 上哪裡出問題 想請各位位大大幫忙解答 謝謝。 : 程式碼(Code):(請善用置底文網頁, 記得排版) : http://ideone.com/mnPsB 我在裡面看到這行 friend std::istream &operator>>(std::istream &is, const complex &t); 所以當你使用 >> 運算子的時候 他假設你傳進去的是 const complex & 而 const 代表裡面的值不可更動,也就是可以想像所有資料成員都是 const 所以 t.real 的型別是 const double 而 c++ 沒有附載 istream& operator>>( istream& in, const double& num ); 這個運算子 所以這一步沒辦法成功: is >> t.real; 講了這麼多其實 解決方法就是 friend std::istream &operator>>(std::istream &is, complex &t); 把 const 去掉 就這樣 記得附載 istream 的 operator>> 要用 nonconst 的 reference 喔^^ P.S. 附載 ostream 的 operator<< 要用 const (有沒有 reference 看情況) 以上 by Aider -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.202.50 ※ 編輯: WJAider 來自: 140.115.202.50 (01/13 16:27)

01/13 16:41, , 1F
謝謝w大 我用const object這樣子物件就不能修改
01/13 16:41, 1F

01/13 16:43, , 2F
我一直以為是我宣告的類別本身不是成員函數XD
01/13 16:43, 2F
文章代碼(AID): #1F3-ewWG (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1F3-ewWG (C_and_CPP)