error: invalid use of member <...> in static member function

Viewed 1478

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]; ^~~~

3 Answers

static functions cannot access member objects. But if you make comp a non-static member function, you can no longer pass it to sort.

A solution is to make comp non-static and wrap it in a lambda in the call to sort, something like:

class Solution {
    map<int, int> freq;
public:
    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(), [this](int a, int b) { return comp(a,b); });
        return nums;
    }
};

In C++, calls to static members functions are not bound to any object and thus does not contain any implicit this pointer. On the other hand, each object receives its own set of data members. In your particular example, freq is a data member, comp is a static member function and frequencySort is a non-static member function. Calls to non-static member functions are bound to an object of the class and thus receive an implicit this pointer. The implicit this pointer is used to access data members inside the member function.

Now, in your particular case, the comp static member function cannot access data members without any explicit access to a class object, because there is no implicit this pointer for static member functions.

Making freq a static data member should solve the issue, and is probably what you want.

Static member functions are not bound to any object i.e., they do not have a this pointer. As a result we cannot refer to this inside the body of a static member function. This restriction applies to both explicit uses of this as well as to implicit uses of this by calling a non-static member.

Solution 1

One way to solve this would be to make freq a static data member. Demo

Solution 2

Another way would be to make comp a non-static member function. Demo. Note that this may lead to other logical errors like comp can no longer be used as a comparator.

Related