0

I am getting odd typo errors that don't make any sense. I am worried this could be a C++ compiler issue (on a Mac with 10.6.8 and Xcode 3.x). If somebody can actually spot the issue, I would be grateful:

template<typename T> int getIdxInVector(const std::vector<T>&  vec, const T& toMatch)
{
std::vector<T>::const_iterator cit = std::find(vec.begin(),vec.end(),toMatch);
return( cit != vec.end() ? cit - vec.begin() : -1 );
}

Here are the errors I am getting:

LooseFunctions.h:27: error: expected `;' before 'cit'
LooseFunctions.h:28: error: 'cit' was not declared in this scope
LooseFunctions.h:27: error: dependent-name 'std::vector<T,std::allocator<_CharT> >::const_iterator' is parsed as a non-type, but instantiation yields a type
LooseFunctions.h:27: note: say 'typename std::vector<T,std::allocator<_CharT> >::const_iterator' if a type is meant

Thanks for any help!

Hans Roggeman
  • 2,300
  • 13
  • 31

1 Answers1

3

const_iterator is a dependent name, so you need to use typename to specify that it refers to a type:

typename std::vector<T>::const_iterator = ...

Note that C++11 makes this easier:

auto cit = std::find(vec.begin(),vec.end(),toMatch);
SirGuy
  • 10,222
  • 2
  • 32
  • 63
juanchopanza
  • 210,243
  • 27
  • 363
  • 452