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<<"("< 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
Answer
your operator <<
should be free function, not Complex
class member in your case.
If you did your operator <<
class member, it actually should take one parameter, which should be stream
. But then you won't be able to write like
std::cout << complex_number;
but
complex_number << std::cout;
which is equivalent to
complex_number. operator << (std::cout);
It is not common practice, as you can note, that is why operator <<
usually defined as free function.
No comments:
Post a Comment