[英]No matching function for call to 'std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)'
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. 我知道这应该是一个众所周知的错了,但我不知道是什么问题。
EDIT: 编辑:
g++ compiler by the way 顺便说一下g ++编译器
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&)'
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 您可以在类内部将其设置为
static
,或者我建议在类外部使用函子或函数
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>);
}
#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. 这可以在g ++中工作,而无需使方法静态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.