gpt4 book ai didi

c++ - 排序 vector 对 : No matching function

转载 作者:太空宇宙 更新时间:2023-11-04 15:32:39 25 4
gpt4 key购买 nike

我已经按照给出的解决方案 here对 vector 对进行排序。

我明白了

linear_problem.h:275:

error: no matching function for call to

‘sort(std::vector<std::pair<int, double> >::iterator,
std::vector<std::pair<int, double> >::iterator, <unresolved overloaded
function type>)’
std::sort(data.begin(), data.end(), compareFunc);

带有代码的类是:

class Coefficients{
private:
std::vector<std::pair<int, double>> data;

public:
Coefficients(){}
bool compareFunc(const std::pair<int, double> &a, const std::pair<int, double> &b){
return a.first > b.first;
}

void sort(){
std::sort(data.begin(), data.end(), compareFunc);
}

};

我不知道哪里出了问题,因为代码与示例非常相似。

最佳答案

compareFunc() 是一个成员函数,需要调用 Coefficients 的实例。

您可以将其设为static 类成员函数来解决该问题:

    static bool compareFunc(const std::pair<int, double> &a, const std::pair<int, double> &b){
// ^^^^^^
return a.first > b.first;
}

void sort(){
std::sort(data.begin(), data.end(), &Coefficients::compareFunc);
// ^^^^^^^^^^^^^^
}

关于c++ - 排序 vector 对 : No matching function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46192596/

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