Home > database >  What's wrong with my comparator? How to fix it?
What's wrong with my comparator? How to fix it?

Time:03-02

I want to find k nearest neighbors of a 2d point from a each point within a vector of point. The comparator is defined as a class and the criteria is the distance of each point of the vector from the inquiry point.

MVW:

#include<iostream>
#include<fstream>
#include<functional>
#include<algorithm>
#include<vector>
#include<deque>
#include<queue>
#include<set>
#include<list>
#include<limits>
#include<string>
#include<memory>

using namespace std; 

  
class random_Point_CCS_xy_generator;

class Point_CCS_xy{
private:
    long double x_coordinate_ {0.0};
    long double y_coordinate_ {0.0};
public:
    Point_CCS_xy () = default;
    Point_CCS_xy(long double x, long double y);
    ~Point_CCS_xy() = default;

    Point_CCS_xy(const Point_CCS_xy& cpyObj);
    Point_CCS_xy(Point_CCS_xy&& moveObj)  noexcept;
    Point_CCS_xy& operator=(const Point_CCS_xy& l_val_RHS);
    Point_CCS_xy& operator=(Point_CCS_xy&& r_val_RHS)  noexcept;

    __attribute__((unused)) inline void set_x_coordinate(long double x);
    __attribute__((unused)) inline void set_y_coordinate(long doubley);
    __attribute__((unused)) inline long doubleget_x_coordinate() const;
    __attribute__((unused)) inline long doubleget_y_coordinate() const;

    bool operator<(const Point_CCS_xy& pnt) const;
    bool operator==(const Point_CCS_xy& RHS) const;

    long double direct_pnt_to_pnt_distance(const Point_CCS_xy& a, const Point_CCS_xy& b) const;
    long double squared_direct_pnt_to_pnt_distance(const Point_CCS_xy& a, const Point_CCS_xy& b) const;

    long double distance_to_this_pnt_sq(const Point_CCS_xy& other) const;

    void print_point_xy () const;

    friend ostream& operator<<(ostream& os, const Point_CCS_xy& pnt);

    vector<Point_CCS_xy> Random_Point_CCS_xy(long double Min, long double Max, size_t n);
    vector<shared_ptr<Point_CCS_xy>> Random_Point_CCS_xy2_(long double Min, long double Max, size_t n);

};

// the comparator under question
class Less_Distance : public std::binary_function<Point_CCS_xy, Point_CCS_xy, bool> {
    Point_CCS_xy& point_;
public:
    explicit Less_Distance(Point_CCS_xy& reference_point)
            : point_(reference_point) {}
    bool operator () (const Point_CCS_xy& a, const Point_CCS_xy& b) const {
        return point_.distance_to_this_pnt_sq(a) < point_.distance_to_this_pnt_sq(b);
    }
};







int main() {


vector<Point_CCS_xy> points {  
            move(Point_CCS_xy(68.83402637, 38.07632221)),
            move(Point_CCS_xy(76.84704074, 24.9395109)),
            move(Point_CCS_xy(16.26715795, 98.52763827)),
            move(Point_CCS_xy(70.99411985, 67.31740151)),
            move(Point_CCS_xy(71.72452181, 24.13516764)),
            move(Point_CCS_xy(17.22707611, 20.65425362)),
            move(Point_CCS_xy(43.85122458, 21.50624882)),
            move(Point_CCS_xy(76.71987125, 44.95031274)),
            move(Point_CCS_xy(63.77341073, 78.87417774)),
            move(Point_CCS_xy(8.45828909,  30.18426696))
    };


cout << "we are here" << endl;
    for(auto p : points) {
        cout << p << endl;
    }

    Point_CCS_xy inquiry_pnt(16.452, 70.258);
    Less_Distance nearer_point(inquiry_pnt);
    vector<Point_CCS_xy> nn_points;

// Problematic part of the code:

    priority_queue<Point_CCS_xy, vector<Point_CCS_xy>, nearer_point> pQ;

for(auto pnt : points) {
        pQ.push(pnt);
    }

   while(!pQ.empty()){
       nn_points.push_back(move(pQ.top()));
       pQ.pop();
   }

    for(auto p : nn_points) {
        cout << p << endl;
    }



return 0;
}

The error is Template argument for template type parameter must be a type. since I am a newbie, any comment would be helpful, thus appreciated.

CodePudding user response:

The ordering argument must be a type, but you're passing an object.
You pass an argument of this type when constructing the queue.

priority_queue<Point_CCS_xy, vector<Point_CCS_xy>, Less_Distance> pQ(nearer_point);
  • Related