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

时间:2018-09-24 11:27:44

标签: c++ std stdvector

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

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链相关我无法推断或追踪的方式...

欢迎任何帮助!

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

您要返回一个迭代器,但是您的函数声明为“ void”

答案 2 :(得分:1)

我认为您忘记了包含<algorithm>

请添加此#include <algorithm>