【问题标题】:vector<string> and find: template argument deduction/substitution failed?vector<string> 并找到:模板参数推导/替换失败?
【发布时间】:2014-04-03 04:48:05
【问题描述】:

我不确定我在这里做错了什么:

vector<string> names;
...

string lh = "localhost";
vector<string>::iterator it = std::find(names.begin(), names.end(), lh);

if(it != names.end())
    names.push_back(lh);

find 的行导致:

../source/ac-pki-4.cpp: In function ‘std::vector<std::basic_string<char> > GetServerAltNames()’:
../source/ac-pki-4.cpp:757:76: error: no matching function for call to ‘find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::basic_string<char>&)’
../source/ac-pki-4.cpp:757:76: note: candidate is:
In file included from /usr/include/c++/4.7/bits/locale_facets.h:50:0,
                 from /usr/include/c++/4.7/bits/basic_ios.h:39,
                 from /usr/include/c++/4.7/ios:45,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from /home/jwalton/ac/include/ac-common.h:33,
                 from ../source/ac-pki-4.cpp:1:
/usr/include/c++/4.7/bits/streambuf_iterator.h:371:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, const _CharT2&)
/usr/include/c++/4.7/bits/streambuf_iterator.h:371:5: note:   template argument deduction/substitution failed:
../source/ac-pki-4.cpp:757:76: note:   ‘__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >’ is not derived from ‘std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >’
make: *** [source/ac-pki-4.o] Error 1

我也没有高兴地尝试了以下:

std::basic_string<char> lh = "localhost";

我尝试了const_iterator,但没有任何乐趣。

下面是我在 Eclipse 下看到的屏幕截图。

任何想法我做错了什么? (为什么在这种情况下使用istreambuf_iterator?)。

【问题讨论】:

  • 似乎其他地方有问题,因为这个例子有效:ideone.com/9CV0tC
  • 您发布的代码没有问题。您能否发布一个无法为您编译的SSCCE

标签: c++ templates c++11 compiler-errors iterator


【解决方案1】:

&lt;iostream&gt; 似乎包含一些具有std::find 重载的标头,这与位于&lt;algorithm&gt; 中的标头不同。为了使用正确的,您必须包含&lt;algorithm&gt;。取消注释此 live example 上的包含行以了解我的意思(以及 &lt;iostream&gt; 以进行进一步调查。)请参阅 libstdc++ 文档。

【讨论】:

  • 好主意。您发布的 coliru 链接确实包括 &lt;algorithm&gt;
  • @Praetorian 我很确定这是错误的原因。因为我一开始忘了包括它。
  • 我同意。我建议您在代码中注释掉 #include &lt;algorithm&gt; 以表明它会生成类似的错误消息。
  • 是的,就是这样……istreambuf_iterator 是从哪里来的?
  • @noloader 真的不知道,但你可以浏览libstdc++ 寻找线索。
猜你喜欢
  • 2018-04-28
  • 2014-09-22
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多