使用 std::find 时没有匹配的函数调用错误

no matching function call error using std::find

本文关键字:函数调用 错误 std find 使用      更新时间:2023-10-16

我有以下函数(用于测试(:

static bool foo(void)
{
std::string name = "name";
std::vector<std::string> test;
std::vector<std::string>::iterator vStart = test.begin();
std::vector<std::string>::iterator vEnd = test.end();
return (std::find(vStart, vEnd, name) == vEnd);
}

我收到一个编译错误:

/data/src/fiware-orion/src/lib/common/string.cpp: In function 'bool foo()':
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: error: no matching function for call to 'find(std::vector<std::basic_string<char> >::iterator&, std::vector<std::basic_string<char> >::iterator&, std::string&)'
return (std::find(vStart, vEnd, name) == vEnd);
^
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: note: candidate is:
In file included from /usr/include/c++/4.9/bits/locale_facets.h:48:0,
from /usr/include/c++/4.9/bits/basic_ios.h:37,
from /usr/include/c++/4.9/ios:44,
from /usr/include/c++/4.9/istream:38,
from /usr/include/c++/4.9/sstream:38,
from /data/src/fiware-orion/src/lib/common/string.cpp:31:
/usr/include/c++/4.9/bits/streambuf_iterator.h:369:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)
find(istreambuf_iterator<_CharT> __first,
^
/usr/include/c++/4.9/bits/streambuf_iterator.h:369:5: note:   template argument deduction/substitution failed:
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: note:   '__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >' is not derived from 'std::istreambuf_iterator<_CharT>'
return (std::find(vStart, vEnd, name) == vEnd);

也许指向问题的消息是这样的:

template argument deduction/substitution failed:

但就我所理解的,find(( 函数参数(std::vector<std::string>::iteratorstd::vector<std::string>::iteratorstd::string(中使用的具体类是清楚的。

特别让我惊讶的是,foo(( 函数的相同代码片段在我的代码的其他部分(即其他.cpp文件(中逐字工作,所以也许它以某种方式与#include链相关,我无法推断或追踪......

欢迎任何帮助!

错误消息中没有来自#include <algorithm>find,只有来自streambuf_iterator.h的。添加#include <algorithm>.

你正在返回一个迭代器,但你的函数声明是'void'

我想你忘了包含<algorithm>

请添加此#include <algorithm>