C++ - How to pass a comparator to STL functions correctly
<p>I wrote a <code>PointCollection</code> class.</p>

<p><code>PointCollection</code> holds a bunch of points. </p>

<p>It has 2 member functions.</p>

<p><code>addPoint(Point point)</code></p>

<p><code>findNearestKPoints(Point center, int k)</code></p>

<p>Each time the <code>findNearestKPoints</code> is called, a <code>center</code> is specified to find k nearest points around it.</p>

<p>But it can't compile:</p>

<p><code>error: called object type 'bool (PointCollection::*)(const Point &amp;, const Point &amp;) const' is not a function or function pointer</code></p>

<p>How to do it correctly?</p>

<p>My code is bellow as a reference:</p>

<pre><code>struct Point {
    int val_;
    Point() {}
    Point(int val) : val_(val) {}
};

class PointCollection {
private:
    vector&lt;Point&gt; points_;
    Point center_;
public:
    PointCollection() {}
    virtual ~PointCollection() {}

    void addPoint(const Point &amp;point) {
        points_.push_back(point);
    }

    bool compare(const Point &amp;a, const Point &amp;b) const {
        return std::abs(a.val_ - center_.val_) &lt; std::abs(b.val_ - center_.val_);
    }

    vector&lt;Point&gt; findNearestKPoints(Point center, int k) {
        center_ = center;

        nth_element(points_.begin(), points_.begin() + k - 1, points_.end(), 
                    &amp;PointCollection::compare);

        return vector&lt;Point&gt;(points_.begin(), points_.begin() + k);
    }
};
</code></pre>