operator<<
クラス内にtypedefがあり、それをで出力できるようにをオーバーロードしたいと思いostream
ます。ただし、コンパイラーはオーバーロードされた演算子を見つけることができません。それが機能するようにどのように宣言できますか?
#include <iostream>
#include <set>
using namespace std;
template <class C>
struct K {
typedef std::set<C> Cset;
Cset s;
// and many more elements here
friend ostream& operator<<(ostream& oo, const Cset& ss){
typename Cset::const_iterator it=ss.begin();
oo << "[";
for(; it!=ss.end(); ++it) oo << (*it) << ",";
oo << "]";
return oo;
}
void DoSomething(){
// do something complicated here
cout << s << endl;
// do something complicated here
}
};
int main(){
K <int> k;
k.s.insert(5);
k.s.insert(3);
k.DoSomething();
}
gccバージョン4.4.520101112(Red Hat 4.4.5-2)(GCC)