Possible Duplicate:
Operator overloading
I didn’t find any thing that could help me in this subject…
I’m trying to over load the << operator, this is my code:
ostream& Complex::operator<<(ostream& out,const Complex& b){
out<<"("<<b.x<<","<<b.y<<")";
return out;
}
this is the declaration in the H file:
ostream& operator<<(ostream& out,const Complex& b);
I get this error:
error: std::ostream& Complex::operator<<(std::ostream&, const Complex&) must take exactly one argument
what and why I’m doing wrong?
thanks
your
operator <<should be free function, notComplexclass member in your case.If you did your
operator <<class member, it actually should take one parameter, which should bestream. But then you won’t be able to write likebut
which is equivalent to
It is not common practice, as you can note, that is why
operator <<usually defined as free function.