<iostream>
seems to be including some headers that have an overload of std::find
that is different from the one that resides in <algorithm>
. In order to use the correct one, you must include <algorithm>
. Uncomment the include line on this live example to see what I mean (and <iostream>
too for further investigation.) See libstdc++ docs.
vector<string> and find: template argument deduction/substitution failed?
-
18-10-2022 - |
Question
I'm not sure what I'm doing wrong here:
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);
The line with find
results in:
../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
I also tried the following with no joy:
std::basic_string<char> lh = "localhost";
And I tried a const_iterator
with no joy.
Below is a screen capture of what I am seeing under Eclipse.
Any ideas what I'm doing wrong? (And why is a istreambuf_iterator
being used in this case?).
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow