Code:
#include<queue>
#include<iostream>
using namespace std;
class CA
{
public:
CA(int parm,int pri):data(parm),priority(pri)
{}
CA()
{}
bool operator < (CA rhs)
{
return data < rhs.GetData();
}
bool pricheck (const CA rhs)
{
return priority < rhs.GetPriority();
}
int GetData()
{
return data;
}
int GetPriority()
{
return priority;
}
private:
int data;
int priority;
};
int main()
{
priority_queue<CA> myq(&CA::pricheck); // Use custom compare function
myq.push(CA(1,2));//(element,priority)
myq.push(CA(2,4));
myq.push(CA(3,5));
myq.push(CA(4,6));
myq.push(CA(5,1));
myq.push(CA(6,3));
//Pop by priority
myq.pop(); //pop 5
myq.pop(); //pop 1
myq.pop(); //pop 6
myq.pop(); //pop 2
myq.pop(); //pop 3
myq.empty();
return 0;
}
main.cpp(20) : error C2662: 'CA::GetPriorit y' : cannot convert 'this' pointer from 'const CA' to 'CA &
main.cpp(40) : error C2664: 'std::priority_ queue<_Ty>::pri ority_queue(con st _Pr &)' : cannot convert parameter 1 from 'bool (__thiscall CA::* )(const CA)' to 'const std::less<_Ty> &'
The objective is to maintain the queue by priority, while retaining the comparison operator overloading for data comparison.
I need some help with this,
p.s- Am using studio 2008
Comment