0

我收到奇怪的拼写错误,没有任何意义。我担心这可能是 C++ 编译器问题(在带有 10.6.8 和 Xcode 3.x 的 Mac 上)。如果有人能真正发现这个问题,我将不胜感激:

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

以下是我得到的错误:

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

谢谢你的帮助!

4

1 回答 1

3

const_iterator是一个从属名称,所以你需要使用typename它来指定它引用一个类型:

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

请注意,C++11 使这更容易:

auto cit = std::find(vec.begin(),vec.end(),toMatch);
于 2013-11-07T16:05:02.123 回答