C++ 需要从';std::pair<;标准::_Rb_tree_迭代器<_Val>;,布尔>;标准::_Rb_tree<_键、\u Val、\u KeyOfValue、\u Compare、\u Alloc>;::_M_insert_unique

C++ 需要从';std::pair<;标准::_Rb_tree_迭代器<_Val>;,布尔>;标准::_Rb_tree<_键、\u Val、\u KeyOfValue、\u Compare、\u Alloc>;::_M_insert_unique,c++,compiler-errors,comparator,stdset,C++,Compiler Errors,Comparator,Stdset,我得到一个编译错误 required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = Solution::EnhancedNode* const&; _Key = Solution::Enha

我得到一个编译错误

required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = Solution::EnhancedNode* const&; _Key = Solution::EnhancedNode*; _Val = Solution::EnhancedNode*; _KeyOfValue = std::_Identity<Solution::EnhancedNode*>; _Compare = Solution::EnhancedNodeComparator; _Alloc = std::allocator<Solution::EnhancedNode*>]'

我是否做了任何明显错误的事情?

在创建一个最小、完整、可验证的TM示例时,我能够解决我的问题。不管怎样,我之所以发布它,是因为谷歌出人意料地在我的引用错误的第一部分中没有点击

原来我不需要中的
&
s

struct EnhancedNodeComparator {
    bool operator() (const EnhancedNode*& a, const EnhancedNode*& b) const {
我是在另一个编译器上编译的

#包括
#包括
使用名称空间std;
树状结构{
int-val;
TreeNode*左;
TreeNode*对;
树节点(intx):val(x),左(nullptr),右(nullptr){
};
类增强节点{
公众:
增强节点(树节点*节点){
节点=节点;
距离=数值限制::max();
丢弃=错误;
}
树节点*node_u2;;
双倍距离;
布尔;
};
结构增强子节点比较器{
布尔运算符()(常量增强节点*&a,常量增强节点*&b)常量{
返回a->distance\ub->distance\ux;
}
};
int main()
{
设置最接近的参数;
最近插入(新增强子节点(新TreeNode(1));
返回0;
}
还有一个更有用的错误:

In file included from /usr/include/c++/4.9/set:60:0,
                 from 3:
/usr/include/c++/4.9/bits/stl_tree.h: In instantiation of 'std::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 = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = EnhancedNode*]':
/usr/include/c++/4.9/bits/stl_tree.h:1498:47:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = EnhancedNode*; _Key = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>]'
/usr/include/c++/4.9/bits/stl_set.h:511:40:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _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 = EnhancedNode*; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<EnhancedNode*>; std::set<_Key, _Compare, _Alloc>::value_type = EnhancedNode*]'
33:54:   required from here
/usr/include/c++/4.9/bits/stl_tree.h:1445:11: error: no match for call to '(EnhancedNodeComparator) (EnhancedNode* const&, EnhancedNode* const&)'
    __comp = _M_impl._M_key_compare(__k, _S_key(__x));
           ^
24:8: note: candidate is:
25:10: note: bool EnhancedNodeComparator::operator()(const EnhancedNode*&, const EnhancedNode*&) const
25:10: note:   no known conversion for argument 1 from 'const key_type {aka EnhancedNode* const}' to 'const EnhancedNode*&'
In file included from /usr/include/c++/4.9/set:60:0,
                 from 3:
/usr/include/c++/4.9/bits/stl_tree.h:1456:7: error: no match for call to '(EnhancedNodeComparator) (EnhancedNode* const&, EnhancedNode* const&)'
       if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
       ^
24:8: note: candidate is:
25:10: note: bool EnhancedNodeComparator::operator()(const EnhancedNode*&, const EnhancedNode*&) const
25:10: note:   no known conversion for argument 1 from 'EnhancedNode* const' to 'const EnhancedNode*&'
In file included from /usr/include/c++/4.9/set:60:0,
                 from 3:
/usr/include/c++/4.9/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, _Arg&&) [with _Arg = EnhancedNode*; _Key = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<EnhancedNode*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/4.9/bits/stl_tree.h:1502:38:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = EnhancedNode*; _Key = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>]'
/usr/include/c++/4.9/bits/stl_set.h:511:40:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _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 = EnhancedNode*; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<EnhancedNode*>; std::set<_Key, _Compare, _Alloc>::value_type = EnhancedNode*]'
33:54:   required from here
/usr/include/c++/4.9/bits/stl_tree.h:1140:8: error: no match for call to '(EnhancedNodeComparator) (EnhancedNode*&, EnhancedNode* const&)'
        || _M_impl._M_key_compare(_KeyOfValue()(__v),
        ^
24:8: note: candidate is:
25:10: note: bool EnhancedNodeComparator::operator()(const EnhancedNode*&, const EnhancedNode*&) const
25:10: note:   no known conversion for argument 1 from 'EnhancedNode*' to 'const EnhancedNode*&'
在/usr/include/c++/4.9/set:60:0中包含的文件中,
从3开始:
/usr/include/c++/4.9/bits/stl_tree.h:在“std::pair std::_Rb_tree::_M_get_insert_unique_pos(const key_type&)[带_key=EnhancedNode*;_Val=EnhancedNode*;_KeyOfValue=std:_Identity;_Compare=EnhancedNodeComparator;_Alloc=std::Alloc=std::分配器;std:_Rb_树::key=EnhancedNode=EnhancedNode*]:
/usr/include/c++/4.9/bits/stl_tree.h:1498:47:std::pair std::_Rb_tree::_M_insert_unique(_Arg&)[with _Arg=EnhancedNode*;_Key=EnhancedNode*;_Val=EnhancedNode*;_KeyOfValue=std:_Identity;_Compare=EnhancedNodeComparator;_Alloc=std::分配器]'
/usr/include/c++/4.9/bits/stl_set.h:511:40:std::pair std::set::insert(std::set::value_type&)[with _Key=EnhancedNode*;_Compare=EnhancedNodeComparator;_Alloc=std::分配器;typename std:_rbtree::const_迭代器=std:_rbtree\u const迭代器;std::Rb tree::set::value_type=EnhancedNode
33:54:从这里开始需要
/usr/include/c++/4.9/bits/stl_tree.h:1445:11:错误:调用'(EnhancedNodeComparator)(EnhancedNode*const&,EnhancedNode*const&)时不匹配
__comp=_M_impl._M_key_compare(u k,_S_key(u x));
^
24:8:注:候选人为:
25:10:注意:bool-EnhancedNodeComparator::operator()(const-EnhancedNode*&,const-EnhancedNode*&)const
25:10:注意:参数1从'const key_type{aka EnhancedNode*const}'到'const EnhancedNode*&'没有已知的转换
在/usr/include/c++/4.9/set:60:0中包含的文件中,
从3开始:
/usr/include/c++/4.9/bits/stl_tree.h:1456:7:错误:调用'(EnhancedNodeComparator)(EnhancedNode*const&,EnhancedNode*const&)不匹配
if(_M_impl._M_key_compare(_S_key(_j._M_node),__k))
^
24:8:注:候选人为:
25:10:注意:bool-EnhancedNodeComparator::operator()(const-EnhancedNode*&,const-EnhancedNode*&)const
25:10:注意:参数1从'EnhancedNode*const'到'const EnhancedNode*&'的转换未知
在/usr/include/c++/4.9/set:60:0中包含的文件中,
从3开始:
/usr/include/c++/4.9/bits/stl_tree.h:In'std::_Rb_tree::iterator std::_Rb_tree::_M_insert_(std:_Rb_tree::_Base_ptr,std:_Rb_tree::_Base_ptr,_Arg&)[with _Arg=EnhancedNode*;_Key=EnhancedNode*;_Val=EnhancedNode*;_KeyOfValue=std::_Identity;_Compare=EnhancedNodeComparator;_Alloc=std::分配器;std:_Rb_tree::iterator=std:_Rb_tree_迭代器;std:_Rb_tree::_Base_ptr=std:_Rb_tree_node*]:
/usr/include/c++/4.9/bits/stl_tree.h:1502:38:std::pair std:_Rb_tree::_M_insert_unique(_Arg&)[with _Arg=EnhancedNode*;_Key=EnhancedNode*;_Val=EnhancedNode*;_KeyOfValue=std:_Identity;_Compare=EnhancedNodeComparator;_Alloc=std::Alloc]
/usr/include/c++/4.9/bits/stl_set.h:511:40:std::pair std::set::insert(std::set::value_type&)[with _Key=EnhancedNode*;_Compare=EnhancedNodeComparator;_Alloc=std::分配器;typename std:_rbtree::const_迭代器=std:_rbtree\u const迭代器;std::Rb tree::set::value_type=EnhancedNode
33:54:从这里开始需要
/usr/include/c++/4.9/bits/stl_tree.h:1140:8:错误:调用'(EnhancedNodeComparator)(EnhancedNode*&,EnhancedNode*const&)时不匹配
||_M_impl._M_key_compare(_KeyOfValue()(_v)),
^
24:8:注:候选人为:
25:10:注意:bool-EnhancedNodeComparator::operator()(const-EnhancedNode*&,const-EnhancedNode*&)const
25:10:注意:参数1从'EnhancedNode*'到'const EnhancedNode*&'的转换未知

我真的不明白为什么会这样。我在网上找到的std::set自定义比较器的所有示例似乎都添加了
&
。如果有人可以解释为答案,我会选择它作为完整答案。

在创建一个最小的、完整的、可验证的TM示例时,我能够解决我的问题。我之所以发布它,是因为令人惊讶的是,谷歌在我引用的第一部分错误中没有点击率

原来我不需要中的
&
s

struct EnhancedNodeComparator {
    bool operator() (const EnhancedNode*& a, const EnhancedNode*& b) const {
我是在另一个编译器上编译的

#包括
#包括
使用名称空间std;
树状结构{
int-val;
TreeNode*左;
TreeNode*对;
树节点(intx):val(x),左(nullptr),右(nullptr){
};
类增强节点{
公众:
增强节点(树节点*节点){
节点=节点;
距离=数值限制::max();
丢弃=错误;
}
树节点*node_u2;;
双倍距离;
布尔;
};
结构增强子节点比较器{
布尔运算符()(常量增强节点*&a,常量增强节点*&b)常量{
返回a->distance\ub->distance\ux;
}
};
int main()
In file included from /usr/include/c++/4.9/set:60:0,
                 from 3:
/usr/include/c++/4.9/bits/stl_tree.h: In instantiation of 'std::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 = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = EnhancedNode*]':
/usr/include/c++/4.9/bits/stl_tree.h:1498:47:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = EnhancedNode*; _Key = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>]'
/usr/include/c++/4.9/bits/stl_set.h:511:40:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _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 = EnhancedNode*; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<EnhancedNode*>; std::set<_Key, _Compare, _Alloc>::value_type = EnhancedNode*]'
33:54:   required from here
/usr/include/c++/4.9/bits/stl_tree.h:1445:11: error: no match for call to '(EnhancedNodeComparator) (EnhancedNode* const&, EnhancedNode* const&)'
    __comp = _M_impl._M_key_compare(__k, _S_key(__x));
           ^
24:8: note: candidate is:
25:10: note: bool EnhancedNodeComparator::operator()(const EnhancedNode*&, const EnhancedNode*&) const
25:10: note:   no known conversion for argument 1 from 'const key_type {aka EnhancedNode* const}' to 'const EnhancedNode*&'
In file included from /usr/include/c++/4.9/set:60:0,
                 from 3:
/usr/include/c++/4.9/bits/stl_tree.h:1456:7: error: no match for call to '(EnhancedNodeComparator) (EnhancedNode* const&, EnhancedNode* const&)'
       if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
       ^
24:8: note: candidate is:
25:10: note: bool EnhancedNodeComparator::operator()(const EnhancedNode*&, const EnhancedNode*&) const
25:10: note:   no known conversion for argument 1 from 'EnhancedNode* const' to 'const EnhancedNode*&'
In file included from /usr/include/c++/4.9/set:60:0,
                 from 3:
/usr/include/c++/4.9/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, _Arg&&) [with _Arg = EnhancedNode*; _Key = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<EnhancedNode*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/4.9/bits/stl_tree.h:1502:38:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = EnhancedNode*; _Key = EnhancedNode*; _Val = EnhancedNode*; _KeyOfValue = std::_Identity<EnhancedNode*>; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>]'
/usr/include/c++/4.9/bits/stl_set.h:511:40:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _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 = EnhancedNode*; _Compare = EnhancedNodeComparator; _Alloc = std::allocator<EnhancedNode*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<EnhancedNode*>; std::set<_Key, _Compare, _Alloc>::value_type = EnhancedNode*]'
33:54:   required from here
/usr/include/c++/4.9/bits/stl_tree.h:1140:8: error: no match for call to '(EnhancedNodeComparator) (EnhancedNode*&, EnhancedNode* const&)'
        || _M_impl._M_key_compare(_KeyOfValue()(__v),
        ^
24:8: note: candidate is:
25:10: note: bool EnhancedNodeComparator::operator()(const EnhancedNode*&, const EnhancedNode*&) const
25:10: note:   no known conversion for argument 1 from 'EnhancedNode*' to 'const EnhancedNode*&'
struct EnhancedNodeComparator {
    bool operator() (EnhancedNode const * const& a, EnhancedNode const * const& b) const {
        return a->distance_ < b->distance_;
    }
};