Reputation: 5103
I'm getting this compiler error when I try to sort my ingredients with my custom comparator.
kitchen.cpp: In member function ‘void Kitchen::printContents(std::ofstream&)’:
kitchen.cpp:172: error: no matching function for call to ‘std::list<Ingredient, std::allocator<Ingredient> >::sort(<unresolved overloaded function type>)’
/usr/include/c++/4.2.1/bits/list.tcc:271: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Ingredient, _Alloc = std::allocator<Ingredient>]
/usr/include/c++/4.2.1/bits/list.tcc:348: note: void std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (Kitchen::*)(const Ingredient&, const Ingredient&), _Tp = Ingredient, _Alloc = std::allocator<Ingredient>]
Here's the code that is causing it:
bool sortFunction(const Ingredient a, const Ingredient b)
{
if (a.getQuantity() < b.getQuantity())
return true;
else if (a.getQuantity() == b.getQuantity())
{
if (a.getName() < b.getName()) return true;
else return false;
}
else return false;
}
void Kitchen::printContents(std::ofstream &ostr)
{
ostr << "In the kitchen: " << std::endl;
ingredients.sort(sortFunction);
std::list<Ingredient>::iterator itr;
for (itr = ingredients.begin(); itr != ingredients.end(); ++itr)
{
ostr << std::setw(3) << std::right << itr->getQuantity() << " "
<< itr->getName() << std::endl;
}
}
Upvotes: 0
Views: 1463
Reputation: 10487
Your sort function is:
bool sortFunction(const Ingredient a, const Ingredient b)
But it should probably be:
bool sortFunction(const Ingredient &a, const Ingredient &b)
(Note the references)
Also, as already mentioned, your Kitchen class already has a function called sortFunction() and it's taking precedence, so either use ::sortFunction() or give each function a unique and more-descriptive name.
If Kitchen::sortFunction() is the one you're wanting, it'll need to be a static member function.
Upvotes: 0
Reputation: 44258
Looks like you have method in Kicthen called sortFunction, and compiler cannot choose proper one. You can try this:
list.sort( ::sortFunction );
To resolve it, or if the function you provided suppose to be method of Kitchen class you need to fix that.
Btw:
if (a.getName() < b.getName()) return true;
else return false;
Is the same as:
return a.getName() < b.getName();
Upvotes: 2
Reputation: 55609
There may be another sortFunction
somewhere (e.g. in Kitchen
), which causes the above error.
Try
ingredients.sort(::sortFunction);
Similar to this question.
Also, for good coding practice, you may want to change
bool sortFunction(const Ingredient a, const Ingredient b)
to
bool sortFunction(const Ingredient &a, const Ingredient &b)
The first is passes in a copy of the object, the second just passes a reference to it.
Upvotes: 3
Reputation: 254461
My guess is that you declare a member function Kitchen::sortFunction
. Within another member function (such as printContents
), that will hide the non-member function you want to use.
The error message suggests that this is the case; it is trying to instantiate sort
for a member-function type bool (Kitchen::*)(const Ingredient&, const Ingredient&)
.
If the member function is not supposed to exist, then remove the declaration. If it is, then either rename one of the functions, or refer to the non-member function as ::sortFunction
.
Upvotes: 1