Type and Dependent Name

Viewed 4453

Manually I can create a std::vector<int>::iterator object like:

std::vector<int>::iterator i;

So here std::vector<int>::iterator is a type. But when I write a function :

template <class T>
std::vector<T>::iterator foo(std::vector<int>::iterator i)
{
    return i;
}

The compiler shows a warning :

std::vector<T>::iterator' : is a dependent name  not a type

and the code does not compiles. But if in main I call the function like this:

int main()
{
    vector<int> v;
    foo(v.begin());
}

The parameter T should be resolved. Then why compiler is showing error?

3 Answers

You have to prefix it with Typename std::vector<T>::iterator is dependent on a template parameter, namely T.Therefore,you should prefix with it typename: Try using:

typename std::vector<T>::iterator

You can refer This :http://pages.cs.wisc.edu/~driscoll/typename.html

Related