26

I'm trying to learn the STL library and I'm having a weird problem. This code compiles perfectly:

void Show(vector<int> myvec)
{
    vector<int>::iterator it;
    cout << "Vector contains:";
    for( it = myvec.begin(); it < myvec.end(); it++) 
    {
         cout << " " << *it;
    }
    cout << endl;
}

while this one gives me an error message at compile time:

template <class T> 
void Show2(vector<T> myvec)
{
    vector<T>::iterator it;
    cout << "Vector contains:";
    for( it = myvec.begin(); it < myvec.end(); it++)
    {
         cout << " " << *it;
    }
    cout << endl;
}

The error is:

$ g++ hello.cpp
hello.cpp: In function ‘void Show2(std::vector<T, std::allocator<_Tp1> >)’:
hello.cpp:19: error: expected ‘;’ before ‘it’
hello.cpp:21: error: ‘it’ was not declared in this scope

It seems a very simple mistake, but I couldn't find it.

Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
  • possible duplicate of [Where to put the "template" and "typename" on dependent names](http://stackoverflow.com/questions/610245/where-to-put-the-template-and-typename-on-dependent-names) – fredoverflow Mar 04 '11 at 11:07
  • 3
    Likely answered a lot before, but hard to search for typename/dependent names if you don't know that such terms exist – Erik Mar 04 '11 at 11:09
  • @FredOverflow that's a very nice link and I'll probably read it right now. But, like @Erik said if you don't know what is the underlying problem, you'll search for many specific terms and never for the more general problem. I did many searches for iterators, templates, vector, STL,... and found nothing I didn't even knew the keyword `typename` existed (:( yeah, I'm a noob :P). I thought it was just a stupid syntax error. – Rafael S. Calsaverini Mar 04 '11 at 11:25
  • That's fine, nobody is complaining ;) – fredoverflow Mar 04 '11 at 11:29

5 Answers5

33

You need to say typename vector<T>::iterator it.

On another note, you're passing vectors by value. That means the entire vector gets copied in the function call. void Show(vector<T> const &myvec) and using const_iterator would be wiser.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
22

You need this:

typename vector<T>::iterator it;

This tells the compiler that vector<T>::iterator should be treated as a type, something it can't assume since iterator is dependent on what T is.

Erik
  • 88,732
  • 13
  • 198
  • 189
4

Some compilers have problems detecting what is a member name and what is a type name, when inside templates. Try writing something like this in the first line of your template function body.

typename vector<T>::iterator it;

CygnusX1
  • 20,968
  • 5
  • 65
  • 109
  • 1
    Correct, but it is not really a compiler problem. The C++ Standard mandates that the compiler treat a dependent name in a template as a member and not a type, unless *typename* is used. – decltype Mar 04 '11 at 11:11
  • 1
    It's not "some compilers". The grammar simply does not allow it. "Some compilers" can double-guess programmer by retrying with typename if parsing fails without it and thus compiling *some* incorrect cases. – Jan Hudec Mar 04 '11 at 11:18
0

Maybe it works using typename vector<T>::iterator it; Your compiler cannot know that there is an inner class iterator.

frast
  • 2,700
  • 1
  • 25
  • 34
0

In the first instance the parameter, although it uses a template, is not a template, it is a fully defined class (vector<int>)

In the latter instance the parameter is a template on type T and thus requires typename

CashCow
  • 30,981
  • 5
  • 61
  • 92