#include <bits/stdc++.h>
using namespace std;
class a{
public:
vector<int> v;
a(){
v = {1,2,3};
}
bool cmp(const int &a, const int &b){
return (a < b);
}
void fun(){
sort(v.begin(), v.end(), cmp);
for(auto x :v ) {
cout << x;
}
}
};
int main() {
a b;
b.fun();
}
why it is giving error at sort(v.begin(), v.end(), cmp);
?
Did you try looking up your error?
Here's one of the results: http://www.programmersought.com/article/5922553195/
like this?
You tell me, does it work?
But why static?
I think the reason is object-oriented programming stuff. Like
sort
is outside the classa
, so it needs an instance ofa
to call itscmp
function, butsort
won't use an instance ofa
so it just can't call the function.But with a static function, apparently just being called inside a member function of
a
is enough forsort
to use the function.All non-static member methods of class has implicit argument $$$*this$$$, so cmp's full signature is $$$(a*, const int&, const int&)$$$, which is not what sort requires