C++ STL list::sort(Compare Comp) compile error: "no matching member function for call to 'sort'   ... where compare function has been templatized.

J F
J F used Ask the Experts™
on
Question is related to C++ STL list::sort(Compare) where Compare is now a template.

I am looking for the correct syntax to override the list::sort(Compare) definition to work with list<pair<int, T> > definition.

//-The Compare Function

template<typename W>
bool weightsort(const pair<int, W> &node1, const pair<int, W> &node2)
{
    return (node1.second <= node2.second);
}


//- Abbreviated class definition containing  STL list definition and call to sort

template<typename T>
class Graph {
    ...
    vector<T> shortest_reach(int startId, T sigWeight) {
        ...
        list<pair<int, T> >                                       frontier;   //-Nodes currently on the Queue to be Explored.
        typename list<pair<int, T> >::iterator      fit;
        ...
            frontier.sort(weightsort);
        ...
    }
    ....
};


$ c++ program.cpp

error: no matching member function for call to 'sort'
            frontier.sort(weightsort);
            ~~~~~~~~~^~~~

usr/include/c++/v1/list:1059:14: note: candidate template ignored:
      couldn't infer template argument '_Comp'
        void sort(_Comp __comp);
             ^
/usr/include/c++/v1/list:1056:10: note: candidate function not viable:
      requires 0 arguments, but 1 was provided
    void sort();
         ^
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Hi,

start by remove this horrible "using namespace std;" that you're hiding, and fully qualify your data types. I suspect some ambiguities.
You're full class definition and dependencies will probably help also.
J F

Author

Commented:
I did as you suggested and I still have the same compiler errors.
bool weightsort()  worked prior to converting it to a template.
Commented:
The call to frontier.sort(weightsort) was missing the template parameter.
The solution is to call  frontier.sort(weightsort<T>) .

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial