No matching function for call to 'std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)'

StackOverflow https://stackoverflow.com/questions/20871801

  •  23-09-2022
  •  | 
  •  

Pregunta

I would like to sort my little list class i wrote for study.

Here is the code:

#include <list>
#include <algorithm>

template<typename T>
class MyList {

private:

    std::list<T> myList;

    bool sortFunc(const T &t1, const T &t2) {
        return (t1 > t2);
    }

public:

     void add(T item) {
         myList.push_back(item);

    }

    void mySort() {
         myList.sort(sortFunc);
    }

};

Short error message:

no matching function for call to 'std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)'

I know it should be a well known mistakbe but i cant figure out what is the problem.

E D I T:

g++ compiler by the way

Error message:

Info: Internal Builder is used for build
g++ -O3 -Wall -c -fmessage-length=0 -o "src\\firstone.o" "..\\src\\firstone.cpp" 
In file included from ..\src\firstone.cpp:2:0:
..\src\mylist.h: In instantiation of 'void MyList<T>::mySort() [with T = int]':
..\src\firstone.cpp:25:17:   required from here
..\src\mylist.h:34:3: error: no matching function for call to 'std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)'
   myList.sort(sortFunc);
   ^
..\src\mylist.h:34:3: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\list:64:0,
                 from ..\src\mylist.h:4,
                 from ..\src\firstone.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\list.tcc:353:5: note: void std::list<_Tp, _Alloc>::sort() [with _Tp = int; _Alloc = std::allocator<int>]
     list<_Tp, _Alloc>::
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\list.tcc:353:5: note:   candidate expects 0 arguments, 1 provided
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\list.tcc:430:7: note: void std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (MyList<int>::*)(const int&, const int&); _Tp = int; _Alloc = std::allocator<int>]
       list<_Tp, _Alloc>::
       ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\list.tcc:430:7: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'bool (MyList<int>::*)(const int&, const int&)'
¿Fue útil?

Solución

Comparator can't be used directly as non-static member

You can make it static inside class or I'd suggest to use a functor or a function outside class

template <typename T>
 bool sortFunc(const T &t1, const T &t2)  {
        return (t1 > t2);
    }

And then, in your class use

void mySort() {
     myList.sort(sortFunc<T>);
}

Otros consejos

#include <functional>

...
void mySort()
{
    myList.sort( std::bind( &MyList<T>::sortFunc, this, std::placeholders::_1, std::placeholders::_2 ) );
}
... 

This works in g++ without the need to make the method static.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top