When compiling :
#include <vector>
template<class T> class foo {
void bar() {
std::vector<T> x;
std::vector<T>::iterator i = x.begin();
}
};
int main() {
return 0;
}
I get :
# g++ ~test.cpp
test.cpp: In member function `void foo<T>::bar()':
test.cpp:7: error: expected `;' before "i"
Shouldn’t this work?
g++ version 3.4.3 on RHEL.
You can, but you need to tell it that
iteratorthere is a type (it doesn’t know, because in general it can depend onT– asvectoris a template type, and could in theory have specializations for someTwhereiteratoris a function or something else). So, you have to usetypenameto indicate that it is always a type: