【发布时间】:2012-02-06 04:56:09
【问题描述】:
我有以下代码无法编译:
#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 编译器在使用std::find 的最后一行大喊:
错误 1 错误 C2678: 二进制 '==' : 未找到需要 'const 类型的左操作数 std::basic_string<_elem>' (或者没有可接受的 转换)c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3171
为什么我不能编译这段代码? 我做错了什么?
更新:完整的编译器输出
1>----- 构建开始:项目:Test01,配置:调试 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 take 'const 类型的左操作数 std::basic_string<_elem>' (或者没有可接受的 转换) 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits, 1>
_Alloc=std::allocator 1> ] 1> 可以是'内置 C++ operator==(const char [6], const char [6])' 1>
c:\程序文件(x86)\微软视觉工作室 11.0\vc\include\exception(488): 或 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 1>
c:\程序文件(x86)\微软视觉工作室 11.0\vc\include\exception(493): 或 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(499): 或 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\program 文件 (x86)\microsoft visual studio 11.0\vc\include\system_error(419):或
'bool std::operator ==(const std::error_code &,const std::error_condition &)' 1> c:\program files (x86)\microsoft 视觉工作室 11.0\vc\include\system_error(427): 或 '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): 或 'bool std::operator ==(const std::tuple &,const std::tuple &)' 1> 尝试 匹配参数列表 '(const std::basic_string<_elem>, const char [6])' 1>
与 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):见 引用函数模板实例化'_InIt std::_Find,const char[6]>(_InIt,_InIt,_Ty (&))' 正在编译 1> 和 1>
[ 1>
_InIt=std::_Tree_unchecked_const_iterator,std::allocator>>>>, 1>
_Mytree=std::_Tree_val,std::allocator>>>, 1> _Ty=const 字符 [6] 1> ] 1>
d:\docs\programming\test01\test01\main.cpp(39) :参见参考 函数模板实例化'_InIt 标准::查找,常量 char[6]>(_InIt,_InIt,_Ty (&))' 正在编译 1> 和 1>
[ 1>
_InIt=std::_Tree_const_iterator,std::allocator>>>>, 1>
_Mytree=std::_Tree_val,std::allocator>>>, 1> _Ty=const 字符 [6] 1> ] ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========
【问题讨论】:
-
奇怪。添加适当的标题等后,这对我来说适用于 GCC。
-
看来MVCPP11 STL实现(或编译器)有问题...
-
请贴出完整代码。
string真的是std::string吗? -
要尝试的几件事:在
std::find调用中将"Hello"显式转换为std::string。将你的函子传递给std::find调用。就目前而言,您的仿函数不会在该调用中使用,它将使用operator==。另外,您能否发布整个构建输出(似乎缺少一些额外的诊断)。 -
@NathanErnst 显式转换没有帮助。
std::find始终使用operator==。添加到原始帖子的构建输出。
标签: c++ compiler-errors find set