在 C++ 中调用指向成员函数的指针时出错

error calling pointer to member function in c++

class Solution {
public:
    bool cmp(pair<int, int>& aa,pair<int, int>& bb){
        if(aa.first<bb.first) return true;
        else if(aa.second<bb.second) return true;
        else return false;
    }
    int maxEnvelopes(vector<pair<int, int> >& envelopes) {
        int i,sz;
        sz=envelopes.size();
        vector<int> v1,v2;
        vector<int>::iterator it1;
        vector<int>::iterator it2;
        sort(envelopes.begin(),envelopes.end(),cmp);
        for(i=0;i<sz;i++){
            it1=lower_bound(v1.begin(),v1.end(),envelopes[i].first);
            it2=lower_bound(v2.begin(),v2.end(),envelopes[i].second);
            if(it1==v1.end()&&it2==v2.end()){
                v1.push_back(envelopes[i].first);
                v2.push_back(envelopes[i].second);
            }
            else{
                v1[it1-v1.end()]=envelopes[i].first;
                v2[it2-v2.end()]=envelopes[i].second;
            }
            //cout<<v1.size()<<" "<<v2.size()<<endl;
        }
        return v1.size();
    }
};

我得到 "error: must use '.*' or '->*' to call pointer-to-member function" 当我在 codeblocks 编译器中编译代码时它会将我重定向到 predefined_ops.h 文件。

您不能使用非 static 成员函数,例如 cmp,作为 sort 的参数。

sort 的参数必须是全局函数、static 成员函数或可调用对象。

为您的程序创建 cmp 一个 static 成员函数。