Can't use a template typename for list?

I wrote a function to return a list<int>::iterator. Everything worked fine. So I decided to make it a template <typename T> function, thus list<T>::iterator. But I get an error saying 'std::list<T>::iterator' : dependent name is not a type.

Here's how the function looks like:

template <typename T>
list<T>::iterator ListSearch (list<T>::iterator first, list<T>::iterator last, const T& target)
{
-----------
-----------
-----------
return iter;
}

I'm following a textbook and I typed the function listed word for word and it still doesn't work. Is it possible to use a template for it?
Don't know why, but when use iterators in templates you need to declare
typename list<T>::iterator // or class list<T>::iterator
so you'll do
1
2
template<class T>
class list<T>::iterator ListSearch (class list<T>::iterator first,class list<T>::iterator last, const T& target)


However it seems that you aren't using the list at all, so you can do this:
1
2
template <typename iterator, typename T>
iterator ListSearch( iterator first, iterator last, const T& target)
Last edited on
The first option didn't work. It had build errors. Something about the arguments doesn't match the list type. I don't know!?

But the 2nd option worked. Good idea. I don't know why it just won't take list<T>::iterator as a return type, instead it took <typename iterator> as a return type. Thanks!
The first option should have worked. Although it's typically done with typename and not class:

1
2
template<class T>
typename list<T>::iterator ListSearch (typename list<T>::iterator first,typename list<T>::iterator last, const T& target)
Last edited on
You're correct. It does work! It's just my compiler that's acting up. For some reason when I type the code, Visual Studio recognizes the error. BUT if I type the code via NOTEPAD, copy it, then paste it on Visual Studio, it bypasses the error. Isn't that odd? Thanks again.
that said...

the second option is probably better anyway because then the function will work with any container type (even straight arrays). Whereas with the first option you can only use lists.
Topic archived. No new replies allowed.