Ambar
Ambar

Reputation: 112

Unusual error while using comparator function for std::sort

I was coding for a topcoder SRM(623) when suddenly the comparator function started giving errors. I saw earlier questions asked on the comparator function but cannot find the error in the following usage:

class CatchTheBeatEasy
{
public:
    bool comp( p p1, p p2){ return (p1.time>p2.time); }
    string ableToCatchAll(vector <int> x, vector <int> y)
    {
        vector < p > points;
        int i=0;
        rep (i,0,x.size())
        {
            p temp;
            temp.time=y[i];
            temp.x=x[i];
            points.push_back(temp);
        }


                    sort(points.begin(),points.end(),comp); //ERROR Here



        int curx=0, curtime=0;
        rep (i,0,points.size())
        {
            if ( points[i].time-curtime < abs(points[i].x-curx) )
                return "Not Able To Catch";
            else
            {
                curtime += abs(points[i].x-curx);
                curx=points[i].x;
            }
        }
        return "Able To Catch";
    }
};

Errors:

  1. error C3867: 'CatchTheBeatEasy::comp': function call missing argument list; use '&CatchTheBeatEasy::comp' to create a pointer to member
  2. error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided

Please help!

As a side node, rep is a macro for "for(i=..;i<..;i++)"

Upvotes: 1

Views: 858

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308530

To use a member function as a comparison function, it must be declared static.

Upvotes: 2

Related Questions