I have the following code that cannot be compiled:
#include <iostream>
#include <set>
#include <functional>
#include <cstring>
using namespace std;
struct StringCompareNoRegister: public binary_function<string, string, bool> {
bool operator()(string const& lhs, string const& rhs) const {
return (_stricmp(lhs.c_str(), rhs.c_str()) < 0);
}
};
int wmain() {
set<string, StringCompareNoRegister> s;
s.insert("hello");
s.insert("STL");
s.insert("Hello");
wcout << s.find("Hello")->c_str() << endl;
wcout << find(s.begin(), s.end(), "Hello")->c_str() << endl;
return 0;
}
MVCPP v.11 CTP compiler yelling on the last line where std::find
is used:
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Alloc>' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3171
Why I cannot compile this code? What have I done wrong?
UPDATE: Full compiler output
1>------ Build started: Project: Test01, Configuration: Debug Win32 ------ 1> main.cpp 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(3171): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Alloc>' (or there is no acceptable conversion) 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits, 1>
_Alloc=std::allocator 1> ] 1> could be 'built-in C++ operator==(const char [6], const char [6])' 1>
c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(488): or 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 1>
c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(493): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(499): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(419): or
'bool std::operator ==(const std::error_code &,const std::error_condition &)' 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(427): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)' 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\tuple(537): or 'bool std::operator ==(const std::tuple<> &,const std::tuple<> &)' 1> while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Alloc>, const char [6])' 1>
with 1> [ 1> _Elem=char, 1>
_Traits=std::char_traits, 1> _Alloc=std::allocator 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(3204) : see reference to function template instantiation '_InIt std::_Find,const char[6]>(_InIt,_InIt,_Ty (&))' being compiled 1> with 1>
[ 1>
_InIt=std::_Tree_unchecked_const_iterator,std::allocator>>>>, 1>
_Mytree=std::_Tree_val,std::allocator>>>, 1> _Ty=const char [6] 1> ] 1>
d:\docs\programming\test01\test01\main.cpp(39) : see reference to function template instantiation '_InIt std::find,const char[6]>(_InIt,_InIt,_Ty (&))' being compiled 1> with 1>
[ 1>
_InIt=std::_Tree_const_iterator,std::allocator>>>>, 1>
_Mytree=std::_Tree_val,std::allocator>>>, 1> _Ty=const char [6] 1> ] ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
string
reallystd::string
? – Sebastian Mach"Hello"
to a std::string in thestd::find
call. Pass your functor to thestd::find
call. As it stands currently, your functor will not be used in that call, it'll useoperator==
. Also, could you post the entire build output (there's some additional diagnostics that appear to be missing). – Nathan Ernststd::find
always useoperator==
. The build output added to the original post. – nickolay