2

I have the class which has list of pointers to objects which is needed to be sorted with comparator which is the member of this class:

class VoronoiDiagram
{
public:
    void BuildVoronoi();

private:
    list<shared_ptr<QPointF>> inputPoints
    QPointF currentVoronoiPoint;

    bool compairingPointsPredictate(shared_ptr<QPointF> a, shared_ptr<QPointF> b) {    float d1 = (a->x()-currentVoronoiPoint.x())*(a->x()-currentVoronoiPoint.x()) + (a->y()-currentVoronoiPoint.y())*(a->y()-currentVoronoiPoint.y());
                                                                                float d2 = (b->x()-currentVoronoiPoint.x())*(b->x()-currentVoronoiPoint.x()) + (b->y()-currentVoronoiPoint.y())*(b->y()-currentVoronoiPoint.y());
                                                                                return d1 < d2; };          
};

And I am calling in BuildVoronoi() this:

inputPoints.sort(compairingPointsPredictate);

But this gives to me two errors: C3867 with my comparator function and C2660 with std::list::sort (do not receive 1 argument).

Sorry for my English or for explanation. I really hope that someone here can help me out. Thanks.

2
  • The problem is that a member function has an implicit first parameter for this, so it doesn't match the signature required for std::list::sort. There are plenty of duplicates out there. Commented Mar 8, 2014 at 21:27
  • So, can u give me the way to fix this? Commented Mar 8, 2014 at 21:54

1 Answer 1

2

sort needs a two argument comparison function but the declaration as member function results in a call similar to:

a.compairingPointsPredicate( b, c)
//                     ^
//                   no 't', it is predicate not predictate ;p

There is no reason to make compairingPointsPredicate a member of a class. You can make it a free standing function (a friend of QPointF if access to private data is required).

class QPointF{

    friend bool compairingPointsPredicate(
                        shared_ptr<QPointF> a, shared_ptr<QPointF> b);          
};


bool compairingPointsPredicate(shared_ptr<QPointF> a, 
                                          shared_ptr<QPointF> b) {    
                                            float d1 = (a->x()-currentVoronoiPoint.x())
                                            *(a->x()-currentVoronoiPoint.x()) + 
                                            (a->y()-currentVoronoiPoint.y())
                                            *(a->y()-currentVoronoiPoint.y());
                                            float d2 = (b->x()-currentVoronoiPoint.x())
                                            *(b->x()-currentVoronoiPoint.x()) +
                                            (b->y()-currentVoronoiPoint.y())
                                            *(b->y()-currentVoronoiPoint.y());
                                            return d1 < d2;
}

usage:

inputPoints.sort(compairingPointsPredicate);
Sign up to request clarification or add additional context in comments.

7 Comments

Can u please give me link to example of how to make friendly function with access to private data and how comparator must be implemented?
Hm... I tried this, but now compairingPointsPredictate "doesn't see" currentVoronoiPoint.
compairingPointsPredictate must have access to a->x() and its currentVoronoiPoint.x(). This is another issue not related to the: error std::list::sort (do not receive 1 argument)
What kind of issue? I don't understand: friendly function must have access to currentVoronoiPoint, but VC2010 just gives to me C2065 and C2228 errors with currentVoronoiPoint. But the main problem is fixed, i guess.
@VIRUS comparator must be a friend of QPointF
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.