【问题标题】:boost::fibonacci_heap: Nested define of handle with comparator redefined circular definition errorsboost::fibonacci_heap:句柄的嵌套定义与比较器重新定义的循环定义错误
【发布时间】:2019-11-07 20:16:29
【问题描述】:

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;
}

【问题讨论】:

    标签: c++ boost fibonacci-heap


    【解决方案1】:

    仅使用operator() 的声明来定义compare_Node。指向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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      相关资源
      最近更新 更多