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

no matching function call error using std::find

我有以下功能(用于测试):

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 链有某种关联在某种程度上我无法推断或追踪...

欢迎任何帮助!

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

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

我想你忘了包括 <algorithm>

请添加这个#include <algorithm>