我跟着article使用stl的priority_queue编码hanffman编码方法,但我认为最终代码中有一些错误或者没有更新。主要问题是priority_queue的声明,我认为应该采用三个参数:priority_queue<节点,矢量,更大> q,而不是priority_queue,更大的> Q值。 但是,即使进行了这些更改,gcc编译器仍会出现如下错误:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h: In member function ‘bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = node]’:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_heap.h:279: instantiated from ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node*, std::vector<node, std::allocator<node> > >, _Distance = long int, _Tp = node, _Compare = std::greater<node>]’
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_heap.h:404: instantiated from ‘void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node*, std::vector<node, std::allocator<node> > >, _Compare = std::greater<node>]’
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_queue.h:367: instantiated from ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = node, _Sequence = std::vector<node, std::allocator<node> >, _Compare = std::greater<node>]’ hanffman.cpp:119: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:218: error: no match for ‘operator>’ in ‘__x > __y’
我不明白。完整代码可用here
答案 0 :(得分:3)
您需要为您的班级定义operator>()
,因为您在std::greater<>
中使用priority_queue
作为比较。我在代码中看到的唯一运算符是operator<()
。
答案 1 :(得分:0)
您缺少节点的>
运算符的定义(您只定义<
运算符)。