小编Len*_*art的帖子

带有自定义比较函数的 std::sort 函数结果错误:必须调用对非静态成员函数的引用

std::sort在类中定义时,我无法将该函数与我的自定义比较函数一起使用。

class Test {
    private:
        vector< vector<int> > mat;
        bool compare(vector<int>, vector<int>);
    public:
        void sortMatrix();
}

bool Test::compare( vector<int> a, vector<int> b) {
    return (a.back() < b.back());
}

void Test::sortMatrix() {
    sort(vec.begin(), vec.end(), compare);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

error: reference to non-static member function must be called
        sort(vec.begin(), vec.end(), compare);
                                     ^~~~~~~
Run Code Online (Sandbox Code Playgroud)

然而compare(),当我sortMatrix()在没有任何类的文件 main.cpp 中定义和时,一切正常。我将不胜感激任何帮助和建议。

c++ sorting reference non-static

11
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1

non-static ×1

reference ×1

sorting ×1