2008-11-24 175 views
2

我有一个函数,它搜索一个STL容器,然后返回迭代器,当它发现位置,但我收到一些有趣的错误信息,可以告诉我我做错了什么?返回一个迭代器

功能:

std::vector<CClass>::iterator CClass::SearchFunction(const std::string& strField) 
{ 
... 

    return it; 

... 
} 

错误:

error C2664: 'std::_Vector_iterator<_Ty,_Alloc>::_Vector_iterator(const std::_Vector_iterator<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'std::_Vector_const_iterator<_Ty,_Alloc> *__w64 ' to 'const std::_Vector_iterator<_Ty,_Alloc> &' 

回答

6

你的搜索功能返回一个常量性。您应该返回相同类型,即std::vector<CClass>::const_iterator,或者将其转换为std::vector<CClass>::iterator,前提是您希望调用方能够通过迭代器修改找到的项目。

编辑︰看到你的更新后,它似乎问题是你的迭代器(它)有一个不同的类型比你的函数返回。他们应该是一样的。

std::vector<CClass>::iterator it; 
+0

谢谢!我知道这很愚蠢。 :) – Konrad 2008-11-24 12:29:34

0

听起来像是你有你的const_iterators混合起来。请张贴更多的代码,特别是你如何声明你的迭代器。

0

你还应该看看std :: find_if()函数。这可能是一个更干净的方式来做到这一点。