Iterator STL with custom template

I have the following boilerplate method:

template <class T> void Class::setData( vector<T> data ) { vector<T>::iterator it; } 

and I get the following compilation error (Xcode / gcc)

error: expected `; 'before' it

I found someone else with a similar problem here (read to see it the same way, even if it starts with a different problem) but they seem to have solved by updating Visual Studio. This makes me guess that this is a compiler problem and that it should compile, is that correct? Iterating through indexing from 0 to size works, however this is not the way I would prefer to implement this function. Is there another way? Thanks

+4
source share
3 answers

The classic use of the typename keyword. We hope you have #include -ed vector and iterator and somewhere in scope. Using:

 typename vector<T>::iterator it; 

Find dependent names. Start here .

+10
source

I think you are missing typename :

 #include <vector> using namespace std; class Class{ public: template <class T> void setData( vector<T> data ) { typename vector<T>::iterator it; } }; 
+1
source

Try:

 template <class T> void Class::setData( std::vector<T> data ) { std::vector<T>::iterator it; } 

Just in this case the using statement is missing?

0
source

All Articles