我仍在尝试实现我自己的LinkedList类版本,现在我遇到了为常量迭代器重载方法的问题.例如,当我尝试使用以下代码打印出列表时:
cout << "citer:" << endl;
for (UberList<int>::CIter it = ulist.begin(); it != ulist.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
Run Code Online (Sandbox Code Playgroud)
我有这些错误:
Error E2034 UberList2.cpp 532: Cannot convert 'UberList<int>::Iter' to 'UberList<int>::CIter' in function main()
Error E2094 UberList2.cpp 532: 'operator!=' not implemented in type 'UberList<int>::CIter' for arguments of type 'UberList<int>::Iter' in function main()
Run Code Online (Sandbox Code Playgroud)
所以据我所知,这意味着使用那些通常的end和begin迭代器方法.以下是我的类中声明这些方法的方法:
Iter begin();
Iter end();
CIter begin() const;
CIter end() const;
Run Code Online (Sandbox Code Playgroud)
和
template<class T>
typename UberList<T>::Iter UberList<T>::begin()
{
Iter it;
it.curr = head; …Run Code Online (Sandbox Code Playgroud)