使用用户定义的对象定义集合(STL 容器)时出现静态断言失败错误 c++17

Aka*_*ane 1 c++ c++17

我在这里定义了一个数据类型为用户定义对象的集合。

\n
#include<bits/stdc++.h>\nusing namespace std;\nclass triplets{\n    public:\n    int x,y,z;\n    triplets(){\n\n    }\n    triplets(int x,int y,int z){\n        this->x=x;\n        this->y=y;\n        this->z=z;\n    }\n\n};\nclass Cmp{\n    public:\n    Cmp(){};\n    bool operator() (const triplets &a, const triplets &b){\n        if( a.x == b.x){\n            return a.y < b.y;\n        }else{\n            return a.x < b.x;\n        }\n    }\n};\nint main(){\n    set<triplets,Cmp>s;\n    s.insert(triplets(2,4,5));\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

该代码在 c++11 和 c++14 版本中编译良好。但它不能在 c++17 及更高版本中编译。它抛出以下错误。

\n
    In file included from /usr/include/c++/11/map:60,\n                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:81,\n                 from Arrays/TwoPointer/test.cpp:1:\n/usr/include/c++/11/bits/stl_tree.h: In instantiation of \xe2\x80\x98static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type) [with _Key = triplets; _Val = triplets; _KeyOfValue = std::_Identity<triplets>; _Compare = Cmp; _Alloc = std::allocator<triplets>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type = const std::_Rb_tree_node<triplets>*]\xe2\x80\x99:\n/usr/include/c++/11/bits/stl_tree.h:2071:47:   required from \xe2\x80\x98std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = triplets; _Val = triplets; _KeyOfValue = std::_Identity<triplets>; _Compare = Cmp; _Alloc = std::allocator<triplets>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = triplets]\xe2\x80\x99\n/usr/include/c++/11/bits/stl_tree.h:2124:4:   required from \xe2\x80\x98std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = triplets; _Key = triplets; _Val = triplets; _KeyOfValue = std::_Identity<triplets>; _Compare = Cmp; _Alloc = std::allocator<triplets>]\xe2\x80\x99\n/usr/include/c++/11/bits/stl_set.h:521:25:   required from \xe2\x80\x98std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = triplets; _Compare = Cmp; _Alloc = std::allocator<triplets>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<triplets, triplets, std::_Identity<triplets>, Cmp, std::allocator<triplets> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<triplets>; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<triplets>, triplets>::rebind<triplets>; typename _Alloc::value_type = triplets; std::set<_Key, _Compare, _Alloc>::value_type = triplets]\xe2\x80\x99\nArrays/TwoPointer/test.cpp:29:13:   required from here\n/usr/include/c++/11/bits/stl_tree.h:770:15: error: static assertion failed: comparison object must be invocable as const\n  770 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,\n      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n/usr/include/c++/11/bits/stl_tree.h:770:15: note: \xe2\x80\x98std::is_invocable_v<const Cmp&, const triplets&, const triplets&>\xe2\x80\x99 evaluates to false\n
Run Code Online (Sandbox Code Playgroud)\n

知道我应该如何定义该集合吗?

\n

joh*_*ohn 5

bool operator() (const triplets &a, const triplets &b){
Run Code Online (Sandbox Code Playgroud)

应该

bool operator() (const triplets &a, const triplets &b) const{
Run Code Online (Sandbox Code Playgroud)