还是不能让这个工作。请帮忙!
template <typename T>
class Container{
public:
...
friend ostream& operator<<(ostream& ostr, const Container<T>& C)
{
for(size_t i=0; i!= data.size(); i++) // ERROR
ostr<<data[i]<<" ";
return ostr;
}
private:
vector<T> data;
};
答案 0 :(得分:4)
data
是C
的成员,因此应该C.data
访问(请记住,您的operator<<
是free function而不是Container
的成员{1}}):
for(size_t i = 0; i != C.data.size(); ++i)
ostr << C.data[i] << " ";