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> &'
4

3 回答 3

6

您的搜索函数返回一个 const_iterator。如果您希望调用者能够通过迭代器修改找到的项目,您应该返回相同的类型,即 ie std::vector< CClass >::const_iterator,或者将其强制转换为 a 。std::vector< CClass >::iterator

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

std::vector< CClass >::iterator it;
于 2008-11-24T12:23:26.807 回答
0

听起来你把你的 const_iterators 弄混了。请发布更多代码,特别是您如何声明迭代器。

于 2008-11-24T12:24:04.903 回答
0

您还应该查看 std::find_if() 函数。这可能是一种更清洁的方法。

于 2008-11-24T14:59:52.750 回答