我有一組整數對的,我想打印出來,所以我重載< <運營商設定和對類如:重載運算符<<爲集
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;
}
當我創建了一套和輸出像
set<pair<int,int>> s;
cout<<s<<endl;
它給人的錯誤:
cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
os<<*it;
和
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)
我不知道是什麼問題,錯誤很神祕。此外,如果我創建一組整數並打印它,它工作正常。
謝謝,實際上,當我爲矢量(而不是集合)寫這個,它工作,這就是爲什麼我感到困惑。 –