2012-09-19 37 views
1

使用fibonacci_heap导致编译错误:使用boost fibonacci_heap

struct Less: public binary_function<Node*, Node*, bool> 
{ 
    bool operator()(const Node*& __x, Node*& __y) const 
     { return __x->time < __y->time; } 

}; 

boost::fibonacci_heap<Node*, Less >* m_heap; 

然后

Less* ls = new Less; 
m_heap = new boost::fibonacci_heap<Node*, Less >(1000, (*ls)); 

任何企图在

no match for call to ‘(TimeSync::Less) (TimeSync::Node* const&, TimeSync::Node*&)’ 
UnmanagedUtils/Trading/Simulation/TimeSync.h:50: note: candidates are: bool TimeSync::Less::operator()(const TimeSync::Node*&, TimeSync::Node*&) const 
/usr/local/include/boost-1_35/boost/property_map.hpp: In function ‘Reference boost::get(const boost::put_get_helper<Reference, PropertyMap>&, const K&) [with PropertyMap = boost::identity_property_map, Reference = unsigned int, K = TimeSync::Node*]’: 
+0

有没有什么原因让你在记忆中做奇怪的事情?即在没有明显原因的情况下在堆上分配数据。 – Wug

+0

我需要Less对象在这个函数的作用域之后仍然活着,但无论如何它没有关系,声明为Less,因为局部变量结果相同 –

+0

此外,调用期望的方法的方法签名指示const修饰符不在正确的地方。尝试改变你的签名为:'bool operator()(Node * const&__x,Node *&__y)const' – Wug

回答

2

更改签名运行m_heap->push(n)结果operator()(Node * const &, Node * const &) const

+0

可以请你解释一下吗?现在错误是不同的 –

+0

你的谓词的'operator()'必须通过const-reference(或者通过value)来获取参数。就这样。您的原始代码具有非常量引用,它不能被数据结构的内部算法所约束,这可能会将元素作为常量引用传递。 –

+0

@RomanNassimov:这就是说,我个人只是将操作符编写为operator()(Node *,Node *)const'。在const-reference中传递指针没有任何好处,因为你知道它们是非常小的类型。 –