Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL iterator with custom template

Tags:

c++

xcode

gcc

stl

i have the following template method,

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

and i'm getting the following compilation error ( XCode/gcc )

error: expected `;' before 'it'

i found someone else with a similar problem here (read down to see it's the same even though it starts out with a different issue) but they seem to have resolved by updating Visual Studio. This makes me guess that it is a compiler issue and that it should compile, is that correct? Iteration via indexing from 0 to size works, however it is not the way i would prefer to implement this function. Is there another way around this? Thanks

like image 536
DavidG Avatar asked Dec 22 '22 12:12

DavidG


2 Answers

Classic case of when to use the typename keyword. Hoping that you have #include-ed vector and iterator and have a using namespace std; somewhere in scope. Use:

typename vector<T>::iterator it;

Look up dependent names. Start here.

like image 61
dirkgently Avatar answered Dec 26 '22 12:12

dirkgently


I think you are missing a typename:

#include <vector>
using namespace std;

class Class{
public:
    template <class T>
    void setData( vector<T> data ) {
        typename vector<T>::iterator it;
    }
};
like image 32
Paolo Tedesco Avatar answered Dec 26 '22 10:12

Paolo Tedesco