具有自定义比较功能的std :: sort函数导致错误:必须调用对非静态成员函数的引用 [英] std::sort function with custom compare function results error: reference to non-static member function must be called
本文介绍了具有自定义比较功能的std :: sort函数导致错误:必须调用对非静态成员函数的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在类中定义std :: sort函数和自定义比较函数时遇到麻烦.
I have trouble using the std::sort function with my custom comparison function when defined inside a class.
class Test {
private:
vector< vector<int> > mat;
bool compare(vector<int>, vector<int>);
public:
void sortMatrix();
}
bool Field::compare( vector<int> a, vector<int> b) {
return (a.back() < b.back());
}
void Test::sortMatrix() {
sort(vec.begin(), vec.end(), compare);
}
我收到以下错误消息:
error: reference to non-static member function must be called
sort(vec.begin(), vec.end(), compare);
^~~~~~~
但是当我在main.cpp文件中没有任何类的情况下定义compare()和sortMatrix()时,一切正常.我将不胜感激.
When I however define compare() and sortMatrix() in the file main.cpp without any class, everything works fine. I would appreciate any help and suggestions.
推荐答案
Make your comparator function static. It will work.
这篇关于具有自定义比较功能的std :: sort函数导致错误:必须调用对非静态成员函数的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文