简体   繁体   中英

C++ Operator overload: stl sort on vector of my custom class

I have a very basic class that is stored inside an STL Vector. I am trying to sort that vector, but am getting cryptic STL errors. Can someone assist?

// Point.h
class Point {
public:
  Point() : x(0), y(0) {}
  Point( float x0, float y0 ) : x(x0), y(y0) {}
  float x;
  float y;
};

// Point.cpp, updated const as per given answers
bool operator< (const Point &p1,const  Point &p2)
{
    return p1.x < p2.x || (p1.x==p2.x && p1.y< p2.y);
}

Again, this Point class is stored in a vector and is being sorted:

std::vector<Point> tmp=N->points;
std::sort(tmp.begin(),tmp.end());

Errors:

http://ideone.com/WIv0u

Can someone point me in the right direction? Thanks!

bool operator< ( const Point &p1, const Point &p2 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM