I need a compare function to use in my priority queue to compare objects. The objects need to be sorted by the ratio of the object. For some reason the RatioCompare function won't work/compile.
The compiler gives the following error:
In member function
bool RatioCompare::operator()(const Item&, const Item&) const:joulethief.cpp:58: error: passing ‘const Item’ as ‘this’ argument ofdouble Item::getRatio()discards qualifiers joulethief.cpp:59: error: passing ‘const Item’ asthisargument ofdouble Item::getRatio()discards qualifiers
Can you guys take a look?
struct RatioCompare
{
bool operator()(const Item &i1, const Item &i2) const
{
double i1value= i1.getRatio();
double i2value= i2.getRatio();
return i1value < i2value;
}
};
Here is the code where I declare it and then test to see if it works within the main...
priority_queue<Item, vector<Item>, RatioCompare > pq;
for(int i=0; i<n; i++)
{
pq.push(tosteal[i]);
}
while(!pq.empty())
{
Item consider= pq.top();
cout<< "Name: "<< consider.getName()<< "Ratio "<< consider.getRatio()<<endl;
pq.pop();
}
I have the both the queue and vector library included in the program as well.
Item::getRatio()asconstqualified. - sjrowlinson