用STL模板重载时的问题
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
#include<iterator>
using namespace std;
class HFTreeNode{
public:
float weight;//节点的权值;
int parent;
int lchild;
int rchild;
char word;
bool operator<(const HFTreeNode& rhs) { return weight < rhs.weight ; }
bool operator>(const HFTreeNode& rhs) { return weight > rhs.weight ; }
};
void main(){
HFTreeNode list[3];
list[0].weight =0.5;
list[0].word ='a';
list[1].weight =0.3;
list[1].word ='b';
list[2].weight =0.4;
list[2].word ='c';
vector<HFTreeNode>vl(list,list+3);
make_heap(vl.begin (),vl.end(),less<HFTreeNode>());
pop_heap(vl.begin (),vl.end());
cout<<"权值最大的为"<<endl;
cout<<(vl.end()-1)->word<<endl;
system("pause");
}
编译显示的错误是
>e:\visual 2010\vc\include\xfunctional(125): error C2678: 二进制“<”: 没有找到接受“const HFTreeNode”类型的左操作数的运算符(或没有可接受的转换)
1> d:\程序演练\stl堆实现优先队列\stl堆实现优先队列\堆.cpp(15): 可能是“bool HFTreeNode::operator <(const HFTreeNode &)”
1> 尝试匹配参数列表“(const HFTreeNode, const HFTreeNode)”时
1> e:\visual 2010\vc\include\xfunctional(124): 编译类 模板 成员函数“bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const”时
1> with
1> [
1> _Ty=HFTreeNode
1> ]
1> d:\程序演练\stl堆实现优先队列\stl堆实现优先队列\堆.cpp(29): 参见对正在编译的类 模板 实例化“std::less<_Ty>”的引用
1> with
1> [
1> _Ty=HFTreeNode
1> ]
1>求大神帮忙改改啊 拙计啊= = STL 重载
[解决办法]
改成
bool operator<(const HFTreeNode& rhs) const { return weight < rhs.weight ; }
bool operator>(const HFTreeNode& rhs) const { return weight > rhs.weight ; }
试试