I have a set of pairs of integers and I want to print it, so I overloaded << operator for set and pair classes as :
template<typename T, typename U>
inline ostream& operator<<(ostream& os, pair<T,U> &p){
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<typename T>
inline ostream& operator<<(ostream& os, set<T> &s){
os<<"{";
for(auto it = s.begin() ; it != s.end() ; it++){
if(it != s.begin())
os<<",";
os<<*it;
}
os<<"}";
return os;
}
When I create a set and output it like
set<pair<int,int>> s;
cout<<s<<endl;
It gives the errors :
cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
os<<*it;
and
initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::pair<int, int>]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
I don't know what is the problem, the errors are very cryptic. Moreover, if I create a set of integers and print it, it works fine.