I'm trying to overload the +
operator for list iterators and unsigned integers (for practice). The code below seems to work fine. To explain what it's supposed to do: if iter
is an iterator and k
an unsigned integer, then iter+k
(or operator+(iter, k)
) returns an iterator obtained from incrementing iter
k-times.
list<int>::iterator operator+(list<int>::iterator iter, unsigned k)
{
while (k != 0)
{
++iter;
--k;
}
return iter;
}
However, when I templatise thus:
template<typename T>
typename list<T>::iterator
operator+(typename list<T>::iterator iter, unsigned k)
{
while (k != 0)
{
++iter;
--k;
}
return iter;
}
and run something simple like
int main(){
list<int> l{1,2};
list<int>::iterator q = l.begin();
q+1;
}
I get a large error that I cannot decipher. I have also tried to no avail:
int main(){
list<int> l{1,2};
list<int>::iterator q = l.begin();
operator+<int>(q,1);
}
Any ideas as to why this is happening and how to fix it would be much appreciated!