[英]Overloading ostream of vector template with iterator
為什么我不能在ostream重載中使用迭代器?
如果我使用迭代方法使用相同的聲明它是有效的。
請考慮以下代碼:
template <class T>
class List {
template <class U>
friend ostream &operator<<(ostream &os, const List<U> &rhs);
private:
vector<T> v;
};
template<class U>
ostream & operator<<(ostream & os, const List<U>& rhs)
{
vector<U>::iterator it = rhs.v.begin();
return os;
}
int main()
{
List<int> list;
cout << list << endl;
return 0;
}
試試吧
typename vector<U>::const_iterator it = rhs.v.begin();
如果你的rsh
是const
,你應該使用const_iterator
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.