Shaikh Hafiz Ahamed Shaikh Hafiz Ahamed - 3 months ago 9
C++ Question

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();
}
};





I am getting
"error: must use '.*' or '->*' to call pointer-to-member function"
And it redirects me to predefined_ops.h file when I compile code in codeblocks compiler.

Answer

You cannot use a non-static member function, such as cmp, as an argument to sort.

The argument to sort has to be a global function, a static member function, or a callable object.

Make cmp a static member function for your program to work.