[英]boost::fibonacci_heap: Nested define of handle with comparator redefined circular definition errors
Boost文檔和先前的堆棧溢出都給出了如何定義自定義比較器函數的有效示例,並在boost堆的節點類型中包含了句柄。 但是,當我將這兩個功能(自定義的比較功能和節點類型中的句柄)組合在一起時,會收到錯誤消息,報告無效使用了'struct compare_Node'的不完整類型。
https://www.boost.org/doc/libs/1_63_0/doc/html/heap/concepts.html#heap.concepts.mutability
除了預定義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;
}
僅使用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;
}
在線演示 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.