-4

I'm tryin to sort an array of objects using std::sort -

sort(convexHull.getSet(), convexHull.getSet()+convexHull.size(),
     [](const Point & a, const Point & b) -> bool
     {             if (a.getX() < b.getX())
         return true;
     else if (a.getX() == b.getX())
         return a.getY() < b.getY();
     else
         return false;; }
);

where convexHull.getSet() returns a pointer to the beginning of an array.

as explained here.

But i'm getting a really long error from my compiler (Clion), something about my assignment operator -

#include <cmath>
#include <string>

using namespace std;

class Point
{

private:
    int _x;
    int _y;
    double _arg;

    void setArg()
    {        
       if(sqrt(_x*_x + _y*_y) == 0)
            _arg = 5;
        else
            _arg = _y / sqrt(_x*_x + _y*_y);
    }

public:
    Point () : Point(0, 0) {}

Point (const int x, const int y) : _x(x), _y(y)
{
    setArg();
}

int getX () const
{ return _x; }

int getY () const
{ return _y; }


    ~Point () {}

    Point& operator= (const Point &rval);
};

(where setArg simply computes a point angle from the positive x-axis).

i've tested the operator using this code -

Point p(5,5);
Point t(3,3);
t=p;
t.setXY(7,7);

and it seems to be working fine.

it's very long so i'll post just part of it -

   In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/algorithm:62:0,
                 from /cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h: In instantiation of 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = const Point*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const Point&, const Point&)> >]':
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:1880:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = const Point*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const Point&, const Point&)> >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:1966:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = const Point*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const Point&, const Point&)> >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:4729:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = const Point*; _Compare = main()::<lambda(const Point&, const Point&)>]'
/cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:75:5:   required from here
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h:1847:17: error: passing 'const Point' as 'this' argument discards qualifiers [-fpermissive]
        *__first = _GLIBCXX_MOVE(__val);
                 ^
In file included from /cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/PointSet.h:9:0,
                 from /cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:4:
/cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/Point.h:45:12: note:   in call to 'Point& Point::operator=(const Point&)'

this might also be relevant -

/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/predefined_ops.h:123:46: note: candidate: bool (*)(Point&, Point&) <conversion>
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/predefined_ops.h:123:46: note:   conversion of argument 3 would be ill-formed:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/predefined_ops.h:123:46: error: binding 'const Point' to reference of type 'Point&' discards qualifiers
/cygdrive/c/Users/o/Desktop/CPP/ex1/cppex1/ex1/ConvexHull.cpp:68:38: note: candidate: main()::<lambda(Point&, Point&)> <near match>
          [](Point & a, Point & b) -> bool
Community
  • 1
  • 1
proton
  • 393
  • 6
  • 31

1 Answers1

1

Apparently the problem is not in the comparison but in what you are passing to std::sort. What is the declaration for

convexHull.getSet()

?

If that for example returns a const Point * then you have a const correctness problem because std::sort needs to be able to write to rearrange elements.

6502
  • 112,025
  • 15
  • 165
  • 265
  • fully agree with your analysis (with the claims of OP, everything works: https://ideone.com/J6TDhU) ! But isn't this more a comment on an incomplete question rather than a definitive answer ? – Christophe Sep 04 '16 at 10:53
  • it seems to be the problemm, I changed getSet()'s signature to return a non-const pointer and now my code compiles. – proton Sep 04 '16 at 10:56