在 c++ 中使用用户定义对象的优先级时遇到问题
因此,我需要为我的学校作业编写霍夫曼压缩/解压缩,并且在使用优先级队列来存储频率时遇到问题。
让我头疼的两个文件是 HCNode.hpp 和 main.cpp。在 HCNode.hpp
文件中,我重载了 bool 运算符<(const HCNode& other)
以及在我的 main.cpp
中,当我尝试像这样初始化优先级队列:
priority_queue< HCNode, vector < HCNode >, less< HCNode> > freq;
编译器向我抛出一堆错误
编辑:这是其中一个错误
/usr/include/c++/4.6/bits/stl_queue.h:391:9:从 'std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) 实例化 [with _Tp = HCNode,_Sequence = std::vector,_Compare = std::less]'
compress.cpp:134:59:从这里实例化
大多数错误似乎是由于与库的某种冲突造成的。
没关系,解决了问题,老师的代码不完整。不过还是感谢那些看过这篇文章的人。
So I need to write the Huffman Compression/Decompression for my school assignment and I'm having trouble using a priority queue to store the frequencies.
The two files that're giving me headaches are HCNode.hpp
and main.cpp
. In the HCNode.hpp
file I've overloaded bool operator<(const HCNode& other)
and in my main.cpp
when I try to initialize a priority queue like this:
priority_queue< HCNode, vector < HCNode >, less< HCNode> > freq;
The compiler throws me a bunch of errors
edit: here is one of the errors
/usr/include/c++/4.6/bits/stl_queue.h:391:9: instantiated from ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = HCNode, _Sequence = std::vector, _Compare = std::less]’
compress.cpp:134:59: instantiated from here
most of the errors seem to be from some sort of conflict with the library.
nevermind, fixed the problem, the teacher's code was incomplete. Thank you to those who looked at this post though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的错误消息不是确切的错误消息,而是导致实际错误的实例化行。
我认为最有可能导致错误的是因为比较器,即运算符<;在本例中, 不是 const 成员函数。
检查它是否是 const 成员函数。
The error message you posted is not the exact error message but the instantiating line which causes the actual error.
The most probable one causing error I think is because the comparator, which is operator< in this case, is not a const member function.
Check if it's a const member function.