Im trying to overload the operator<<
const ostream & operator<<(const ostream& out, const animal& rhs){
out << rhs.a;
return out;
}
it seems that im getting an error because im return a const and also because the first argument is const refrence to an ostream object.
cout << objectOfAnimal1 << objectOfAnimal2 ;
it work just fine if I change the the return type and the operator signature to this one:
ostream & operator<<(ostream& out, const animal& rhs)
You need to have:
ostream & operator<<(ostream& out, const animal& rhs)
In your code You are trying to modify a const ostream object, and so you get the errors.
It should not be const.
ostream & operator<<(ostream& out, const animal& rhs){
out << rhs.a;
return out;
}
You've already explained what is probable reason of problem and you really didn't try it out?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With