I have the following operator-overloading prototypes:
ostream& operator<<(ostream & outputstream, my_arr& arr)
my_arr operator+(const my_arr& left, const my_arr& right)
I call:
cout << (arr1 + arr2);
This gives me the following compiler error:
error: no match for ‘operator<<’ in ‘std::cout << operator+(const my_array&, const my_array&)((*(const my_array*)(& y)))’
This goes away if I change the signature of << to the following:
ostream& operator<<(ostream & outputstream, const my_arr& arr)
I might be missing something basic here, but why does this happen?
Thanks for your help.
The problem is that when passing as reference, you cannot pass “temporary” (rvalue) objects such as the result of an addition. When passing a const reference, C++ rules allow passing temporaries because it’s guaranteed they won’t be written to.