Can someone figure out the error on sort function- Line 12: invalid use of non-static member function

  • 0
    class Solution {
        bool compareNum(int i, int j){
            string x=to_string(i);
            string y=to_string(j);
            string a=x+y;
            string b=y+x;
            return > 0 ? 1 : 0;
        string largestNumber(vector<int> &num) {
            string res="";
            for(int i=0;i!=num.size();i++)
                res=res + to_string(num[i]);
            return res;

  • 1

    Make it static bool compareNum, it should work. sort will only take a static function pointer as an argument

  • 0

    It worked. Thanks. But I never use static keyword in compare function for sort and it seems to work everytime.

  • 0

    It works without static keyword when I define the function outside of the class.
    static keyword is needed when compare function is a class member.
    Not really sure why. Will check this further. Anyways thanks.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.