Custom list.sort comparisons in C++ -
i have std::list i'm trying sort based on calculations. point2d struct int no, double x, , double y;
here's method contains list.sort code:
std::vector<point2d> grahamscan::getsortedpointset(std::vector<point2d> points) { point2d lowest = getlowestpoint(points); std::list<point2d> list; (int = 0; < (int)points.size(); i++) { list.push_back(points[i]); } list.sort(compare_points); std::vector<point2d> temp; (int = 0; < (int)list.size(); i++) { temp.push_back(list.front()); list.pop_front(); } return temp; }
and here's compare_points method wrote:
bool grahamscan::compare_points(const point2d& a, const point2d& b) { if (a.x == b.x && a.y == b.y) { return false; } double thetaa = atan2((long)a.y - lowest.y, (long)a.x - lowest.x); double thetab = atan2((long)b.y - lowest.y, (long)b.x - lowest.x); if (thetaa < thetab) { return false; } else if (thetaa > thetab) { return true; } else { double distancea = sqrt((((long)lowest.x - a.x) * ((long)lowest.x - a.x)) + (((long)lowest.y - a.y) * ((long)lowest.y - a.y))); double distanceb = sqrt((((long)lowest.x - b.x) * ((long)lowest.x - b.x)) + (((long)lowest.y - b.y) * ((long)lowest.y - b.y))); if (distancea < distanceb) { return false; } else { return true; } } }
the error visual studio spitting out @ me "grahamscan::compare_points":non-standard syntax; use '&' create pointer member"
i don't have experience in c++, i'm trying convert java code uses treeset c++ , attempt.
any assistance appreciated.
if want keep compare_points
in grahamscan
namespace need make static:
static bool grahamscan::compare_points
the reason compiler complains compare_points
member function. needs grahamscan
object applied on. behind curtains real function signature of compare_points
bool compare_points(grahamscan *this, const point2d& a, const point2d& b)
. either make static or don't define member function.
once make compare_points static, lowest variable no longer accessible it. easier way work around make lowest static:
class grahamscan { // declaration inside class static point2d lowest; } // definition outside class point2d grahamscan::lowest;
and use this:
std::vector<point2d> grahamscan::getsortedpointset(std::vector<point2d> points) { grahamscan::lowest = getlowestpoint(points); //... }