【C++】一次遇到的需要加入const属性的情况

我的问题:
在写仿函数的时候我是这样写的:

//仿函数
class MyCompare
{
public:
	bool operator()(int v1, int v2)//重载()
	{
		return v1 > v2;
	}
};

报错:

已启动生成…
1>------ 已启动生成: 项目: STL, 配置: Debug Win32 ------
1>41set容器-排序.cpp
1>D:\visual studio\Microsoft Visual Studio2019Community\VC\Tools\MSVC\14.28.29910\include\xutility(1622,19): error C3848: 具有类型“const MyCompare”的表达式会丢失一些 const-volatile 限定符以调用“bool MyCompare::operator ()(int,int)1>D:\visual studio\Microsoft Visual Studio2019Community\VC\Tools\MSVC\14.28.29910\include\xutility(1621): message : 查看对正在编译的函数 模板 实例化“bool std::_Debug_lt_pred<const MyCompare&,const _Kty&,const _Keyty&,0>(_Pr,_Ty1,_Ty2) noexcept(<expr>)”的引用
1>        with
1>        [
1>            _Kty=int,
1>            _Keyty=int,
1>            _Pr=const MyCompare &,
1>            _Ty1=const int &,
1>            _Ty2=const int &
1>        ]
1>D:\visual studio\Microsoft Visual Studio2019Community\VC\Tools\MSVC\14.28.29910\include\xtree(1002): message : 查看对正在编译的函数 模板 实例化“std::_Tree_find_result<std::_Tree_node<int,std::_Default_allocator_traits<_Alloc>::void_pointer> *> std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::_Find_lower_bound<_Key>(const _Keyty &) const”的引用
1>        with
1>        [
1>            _Alloc=std::allocator<int>,
1>            _Kty=int,
1>            _Pr=MyCompare,
1>            _Key=int,
1>            _Keyty=int
1>        ]
1>D:\visual studio\Microsoft Visual Studio2019Community\VC\Tools\MSVC\14.28.29910\include\xtree(1009): message : 查看对正在编译的函数 模板 实例化“std::_Tree_find_result<std::_Tree_node<int,std::_Default_allocator_traits<_Alloc>::void_pointer> *> std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::_Find_lower_bound<_Key>(const _Keyty &) const”的引用
1>        with
1>        [
1>            _Alloc=std::allocator<int>,
1>            _Kty=int,
1>            _Pr=MyCompare,
1>            _Key=int,
1>            _Keyty=int
1>        ]
1>D:\visual studio\Microsoft Visual Studio2019Community\VC\Tools\MSVC\14.28.29910\include\xtree(1344): message : 查看对正在编译的函数 模板 实例化“std::pair<std::_Tree_node<int,std::_Default_allocator_traits<_Alloc>::void_pointer> *,bool> std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::_Emplace<int>(int &&)”的引用
1>        with
1>        [
1>            _Alloc=std::allocator<int>,
1>            _Kty=int,
1>            _Pr=MyCompare
1>        ]
1>D:\visual studio\Microsoft Visual Studio2019Community\VC\Tools\MSVC\14.28.29910\include\xtree(1344): message : 查看对正在编译的函数 模板 实例化“std::pair<std::_Tree_node<int,std::_Default_allocator_traits<_Alloc>::void_pointer> *,bool> std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::_Emplace<int>(int &&)”的引用
1>        with
1>        [
1>            _Alloc=std::allocator<int>,
1>            _Kty=int,
1>            _Pr=MyCompare
1>        ]
1>C:\Users\matebook\Desktop\C++练习\STL\41set容器-排序.cpp(35): message : 查看对正在编译的函数 模板 实例化“std::pair<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<int>>>,bool> std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::insert<false,0>(int &&)”的引用
1>        with
1>        [
1>            _Kty=int,
1>            _Pr=MyCompare,
1>            _Alloc=std::allocator<int>
1>        ]
1>C:\Users\matebook\Desktop\C++练习\STL\41set容器-排序.cpp(35): message : 查看对正在编译的函数 模板 实例化“std::pair<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<int>>>,bool> std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::insert<false,0>(int &&)”的引用
1>        with
1>        [
1>            _Kty=int,
1>            _Pr=MyCompare,
1>            _Alloc=std::allocator<int>
1>        ]
1>已完成生成项目“STL.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

const MyCompare

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值