作者snoopy0907 (超強氣)
看板C_and_CPP
標題[問題] 只能一個參數?
時間Fri Apr 13 21:13:21 2012
小弟有各 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
推 purpose:書上不是這樣教的吧,你再去跟書上的範例比對 04/13 21:47
推 nowar100:那不是 member function 把她拉到外面去 04/13 21:48
→ snoopy0907:謝謝~原來如此 04/13 22:55
推 LPH66:一樓大概想說的是 friend... 04/14 00:55