C++ STL priority_queue 元素的运算符重载问题 , 当元素为模板类是运算符重载编译不过

#include<stdio.h>#include<algorithm>#include<set>#include<map>#include<vector>#includ... #include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <string>
#include <queue>
#include <math.h>
using namespace std;
template<class T>
class A
{
public:
A():k(0){}
A(T a):k(a){}
bool operator < (const A<T> &a)
{
return k < a.k;
}
T k;
};
int main()
{
priority_queue< A<int> > a;
a.push(12);
return 0;
}
展开
 我来答
_whales
2013-02-03 · TA获得超过2281个赞
知道大有可为答主
回答量:1814
采纳率:85%
帮助的人:536万
展开全部
类内定义不要使用模板语法或者
template A(const
A<U>& a);
并且要写 friend class< U>;还不一定能行
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
reverse_walker
2013-02-03 · TA获得超过590个赞
知道小有建树答主
回答量:377
采纳率:0%
帮助的人:473万
展开全部
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <string>
#include <queue>
#include <math.h>
using namespace std;
template<class T>
class A
{
T k;
public:
A():k(0){}
A(T a):k(a){}

bool operator<( const A& a ) const // 这里加个const就好了
{
return k < a.k;
}
};
int main()
{
priority_queue< A<int> > a;
a.push( 12 );
return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式