I was trying to return an iterator to a vector from a template function (not yet a template class member--I'm still writing that). The compiler kept giving me errors (copied below to to facilitate Google searches). I knew basically what the problem was, but the exact syntax was elusive.
I read the internet, searched SO, including Where and why do I have to put the "template" and "typename" keywords?, but didn't find an answer that worked. I figured I should ask the question and answer it myself here.
(Abbreviated) original code follows:
#include <vector>
#include <algorithm> // std::lower_bound
template<typename T> std::vector<T>::iterator
insertIntoVector(std::vector<T>& vec, const T& val)
{ itr = [some std::vector<T> iterator];
return itr; // return iterator to val in vec.
} // End of insertIntoVector();
Compiler errors:
error C2145: syntax error: missing ';' before identifier 'insertIntoVector'
error C2065: 'T' : undeclared identifier
error C2923: 'std::vector' : 'T' is not a valid template type argument for parameter '_Ty'
Getting wise, I tried this:
template<typename T> typename std::vector<T>::iterator
insertIntoVector(std::vector<T>& vec, const T& val)
More Compiler errors:
error C1075: end of file found before the left brace '{'
I'll post my answer below if this question gets unlocked. Otherwise, see my answer on Where and why do I have to put the "template" and "typename" keywords?.