[問題] 只能一個參數?

看板C_and_CPP (C/C++)作者 (超強氣)時間14年前 (2012/04/13 21:13), 編輯推噓3(301)
留言4則, 4人參與, 最新討論串1/1
小弟有各 CLASS 但是在重載<<時 一直有個錯誤 std::ostream如下& Complex::operator<<(std::ostream&, Complex)’ must take exactly one argument 可是一般重載<<時 不都是設一個OSTREAM嗎? 還是這是g++的限制呢? 謝謝大家指教 #include<iostream> class Complex { private: double re, im; public: Complex(double r, double i): re(r), im(i) {} Complex operator+(const Complex&); std::ostream& operator<<(std::ostream& out, const Complex x); }; Complex Complex::operator+(const Complex& a) { Complex c(a.re+re,a.im+im); return c; } std::ostream& Complex::operator<<(std::ostream& out, const Complex x) { out << x.re << " " << x.im; return out; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.60.0.221

04/13 21:47, , 1F
書上不是這樣教的吧,你再去跟書上的範例比對
04/13 21:47, 1F

04/13 21:48, , 2F
那不是 member function 把她拉到外面去
04/13 21:48, 2F

04/13 22:55, , 3F
謝謝~原來如此
04/13 22:55, 3F

04/14 00:55, , 4F
一樓大概想說的是 friend...
04/14 00:55, 4F
文章代碼(AID): #1FY2PqpT (C_and_CPP)
文章代碼(AID): #1FY2PqpT (C_and_CPP)