I'm encountering this error when I'm trying to use the content of my map 'freq' declared as a class variable, inside my comparator function for sorting the vector(using map to sort the vector elements by frequency). Can anyone highlight where am i going wrong and what should I do? Here's the code:
class Solution {
map<int, int> freq;
public:
static bool comp(int a, int b){
if(a!=b){
int c1 = freq[a];
int c2 = freq[b];
if(c1!=c2)
return c1<c2;
else{
return a>b;
}
}
return a>b;
}
vector<int> frequencySort(vector<int>& nums) {
for(int i:nums){
freq[i]++;
}
sort(nums.begin(), nums.end(), comp);
return nums;
}
};
Error is: Line 6: Char 22: error: invalid use of member 'freq' in static member function int c1 = freq[a]; ^~~~