need help with a simple template function

I have this dummy template function that takes a vector of some type T, and returns the first element by using an iterator. This is just a simplification of what I'm trying to do so please don't focus on the fact that this function is useless:

1
2
3
4
5
template<class T>
T myfun(vector<T>& vec) {
    vector<T>::iterator it = vec.begin(); //error: "expected ';' before 'it'"
    return *it;
}


Would really appreciate any advice as to what it is that I'm doing wrong...
Last edited on
Ok I found a solution, I needed to use the typename keyword:
1
2
3
4
5
template<class T>
T myfun(vector<T>& vec) {
    typename vector<T>::iterator it = vec.begin(); //compiles fine
    return *it;
}
You and I were working on the same problem this evening, but I can't get my calls to my template function to compile. Could you post a line of code with a call to your template function, please? Maybe it will help me figure out what I might be doing wrong.
supposing I have a vector of integers:

1
2
3
4
5
6
vector<int> myvec;
myvec.push_back(1);
myvec.push_back(2);

// now return the last value
int last_val = myfun<int>(myvec);
Topic archived. No new replies allowed.