boost :: fibonacci_heap:具有比较器的句柄的嵌套定义重新定义了循环定义错误

时间:2019-06-25 23:49:31

标签: c++ boost fibonacci-heap

Boost文档和先前的堆栈溢出都给出了如何定义自定义比较器函数的有效示例,并在boost堆的节点类型中包含了句柄。但是,当我同时使用这两个功能(自定义的比较功能和节点类型中的句柄)时,会收到错误消息,报告无效使用了'struct compare_Node'的不完整类型。

https://www.boost.org/doc/libs/1_63_0/doc/html/heap/concepts.html#heap.concepts.mutability

Using boost fibonacci_heap

Defining compare function for fibonacci heap in boost

Decrease operation in fibonacci heap, boost

除了预定义Node和compare_Node的结构外,我不确定在解决圆形问题的同时仍将句柄作为Node结构中的成员安全地持有。

#include <boost/heap/fibonacci_heap.hpp>

struct compare_Node; //predefine to avoid circular issues
struct Node; //predefine to avoid circular issues

using fib_heap = boost::heap::fibonacci_heap<struct Node*,
    boost::heap::compare<struct compare_Node>>;

// 6-byte struct total
struct Node {
    double value; 

    fib_heap::handle_type* handle; // orig
};

// override for min_heap
struct compare_Node
{
    bool operator() (struct Node* const n1, struct Node* const n2) const
    {
        return n1->value > n2->value;
    }
};

int main() {
    fib_heap heap;
    return 0;
}

1 个答案:

答案 0 :(得分:0)

仅用声明compare_Node定义operator()。指向Node的指针不需要Node定义。在Node定义之后,您可以添加operator()的正文:

struct compare_Node
{
    bool operator() (struct Node* const n1, struct Node* const n2) const;
};

using fib_heap = boost::heap::fibonacci_heap<struct Node*,
    boost::heap::compare<struct compare_Node>>;

// 6-byte struct total
struct Node {
    double value; 

    fib_heap::handle_type* handle; // orig
};

bool compare_Node::operator() (struct Node* const n1, struct Node* const n2) const
{
        return n1->value > n2->value;
}

Online demo