0

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;

    }
}
4
  • how are you defining sort? The error is saying that you don't have a matching call to it. Commented Feb 28, 2013 at 18:21
  • 1
    @TopGunCoder: std::list has a sort() function that takes a comparator. Commented Feb 28, 2013 at 18:25
  • 2
    Do you have any other function named sortFunction? Is sortFunction a non-static member of Kitchen? Commented Feb 28, 2013 at 18:31
  • 2
    Why do you have both bool sortFunction(const Ingredient a, const Ingredient b) and also bool Kitchen::sortFunction(const Ingredient&, const Ingredient&)? Commented Feb 28, 2013 at 18:54

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure that the reason?
I had it like that before and it was giving me the same error. : (
2

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();

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.