gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:38:06 31 4
gpt4 key购买 nike

当在类中定义时,我无法将 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);
}

我收到以下错误消息:

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

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

最佳答案

Make your comparator function static. It will work.

关于c++ - 具有自定义比较函数结果错误的 std::sort 函数:必须调用对非静态成员函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37767847/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com