简体   繁体   English

没有匹配的函数来调用&#39;std :: list <int, std::allocator<int> &gt; :: sort( <unresolved overloaded function type> )&#39;

[英]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.

相关问题 没有匹配的函数来调用&#39;bind( <unresolved overloaded function type> ,const std :: _ Placeholder &lt;1&gt;&,int *) - no matching function for call to ‘bind(<unresolved overloaded function type>, const std::_Placeholder<1>&, int*) 没有用于调用std :: transform,未解析的重载函数类型的匹配函数 - No matching function for call to std::transform, unresolved overloaded function type C ++模板错误:调用std :: vector没有匹配功能 <int, std::allocator<int> &gt; - C++ Templates Error: no matching function for call std::vector<int, std::allocator<int> > “没有匹配函数来调用'async(std :: launch,<unresolved overloaded function type>,std :: string&)'” - “no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’” 没有匹配的函数来调用&#39;merge(std :: vector <int> &,std :: vector <int> &) - No matching function for call to 'merge(std::vector<int>&, std::vector<int>&) 如何解决错误:“没有匹配的函数来调用&#39;bind( <unresolved overloaded function type> 在类中使用std :: bind时 - How to solve the error: “no matching function for call to ‘bind(<unresolved overloaded function type>” when std::bind is used in a class C++ 多线程错误:没有匹配的 function 调用 'std::thread::thread(<unresolved overloaded function type></unresolved> - C++ Multithreading Error : no matching function for call to 'std::thread::thread(<unresolved overloaded function type> 错误:没有匹配的函数可以调用&#39;sort(...,…, <unresolved overloaded function type> )&#39; - error: no matching function for call to 'sort(…, …, <unresolved overloaded function type>)' 的std ::线程 <unresolved overloaded function type> 错误 - std::thread <unresolved overloaded function type> error std::transfrom 中未解决的重载 function 类型 - Unresolved overloaded function type in std::transfrom
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM