I'm trying to compile a simple function template however am running into compilation errors. Does anyone know whats wrong?
#include <iostream>
#include <vector>
using namespace std;
template<class T> void f1(vector<T>& arg)
{
for (vector<T>::iterator p = arg.begin();
p != arg.end();
++p)
cout << *p << endl;
}
int main(int argc, char *argv[])
{
return 0;
}
Compilation errors:
g++ -pedantic -Wall test102.cc && ./a.out
test102.cc: In function ‘void f1(std::vector<T>&)’:
test102.cc:7:10: error: need ‘typename’ before ‘std::vector<T>::iterator’ because ‘std::vector<T>’ is a dependent scope
for (std::vector<T>::iterator p = arg.begin();
^~~
test102.cc:7:34: error: expected ‘;’ before ‘p’
for (std::vector<T>::iterator p = arg.begin();
^~
;
test102.cc:8:3: error: ‘p’ was not declared in this scope
p != arg.end();
^
UPDATE:
I found the error, I copied from Stroustrup C++ 4th Ed. Page 163 and it's errata s/for (vector/for (typename vector/
. Does anyone know why typename is required?