4

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

2
  • 1
    Note: consider passing in "vector<T> &data", or maybe "vector<T> const &data" not "vector<T> data". The former two will pass in a reference to the vector, the latter will make a full copy of the vector. Commented Feb 27, 2009 at 13:16
  • Of course, it was just an example :) Commented Mar 2, 2009 at 11:26

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }
};

Comments

0

Try:

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

Just is case it's a missing using statement?

1 Comment

If he had missed the using statement he would also get an error on the function signature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.