I'm making an application that sorts a vector of objects. These objects are Cartesian points, defined in the following class:
class CartesianPoint {
private:
int x; // Coordinate X of a pin or a wire part on a cartesian plan
int y; // Coordinate Y of a pin or a wire part on a cartesian plan
public:
CartesianPoint();
CartesianPoint(const CartesianPoint& orig);
virtual ~CartesianPoint();
int getX() const {
return x;
}
int getY() const {
return y;
}
CartesianPoint(int x, int y) :
x(x), y(y) {
}
To sort, I'm using the sort function as follows:
int main()
{
int i;
CartesianPoint *a = new CartesianPoint(10, 13), *b = new CartesianPoint(32, 1), *c = new CartesianPoint(5, 29), *d = new CartesianPoint(12, 6), *e = new CartesianPoint(21, 100);
CartesianPoint myPoints[] = {*a, *b, *c, *d, *e};
std::vector<CartesianPoint> myvector(myPoints, myPoints+8);
// using function as comp
std::sort(myvector[0], myvector.end(), myfunction);
// print out content:
std::cout << "myvector contains:";
for (i = 0; i < 8; i++)
std::cout << ' ' << '(' << myvector[i].getX() << ',' << myvector[i].getY() << ')';
std::cout << '\n';
return 0;
}
where "myfunction" is the function that sorts the points in descending order with respect to x:
CartesianPoint* myfunction(CartesianPoint *a, CartesianPoint *b) {
if(a->getX() > b->getX())
return (a);
else
return (b);
}
The problem you are experiencing is the call of the sort
function:
sort.cpp:33:5: error: no matching function for call to 'sort'
std::sort(myvector[0], myvector.end());
What can be wrong?