3

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!

Joseph
  • 12,678
  • 19
  • 76
  • 115
  • 1
    Try const Point & as the argument to the overload. – ds1848 Apr 25 '12 at 02:51
  • You seem to have a misunderstanding on how to use ideone.com. You put your *program* there, and it will compile and generate the error messages for you. No need to paste the error messages themselves. – Mark Ransom Apr 25 '12 at 03:08
  • @MarkRansom: You can't have multiple source and header files on ideone, as far as I'm aware. – Benjamin Lindley Apr 25 '12 at 03:11
  • @BenjaminLindley, you're probably right, but you can insert the contents of the .h at the point where you'd do the `#include`. – Mark Ransom Apr 25 '12 at 03:13
  • @MarkRansom: He would, presumably, also have to insert the contents of his Point implementation file, in which case, he would not have had the same errors. – Benjamin Lindley Apr 25 '12 at 03:19

1 Answers1

6

bool operator< (constPoint &p1,constPoint &p2 )

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Just tried this, I also had to move the operator implementation to a .cpp file because g++ was complaining about duplicate definitions- still no dice. Getting crazy amounts of errors, I will update the original post in a second with a pastebin of the errors. – Joseph Apr 25 '12 at 02:55
  • @Joseph: After you moved the implemetation to the cpp file, do you have a declaration(the signature without the body) in the header? visible in the place where you use `std::sort`? – Benjamin Lindley Apr 25 '12 at 02:59
  • I'm sorry, can you clarify that? In the code where I am using std::sort, I have included "Point.h" and I am compiling g++ main.cpp otherstuff.cpp Point.cpp. Is that what you are asking? – Joseph Apr 25 '12 at 03:01
  • 1
    Better than moving it to another file may be to add `inline` to the function: `inline bool operator<( const Point &p1, const Point &p2)`. – Michael Anderson Apr 25 '12 at 03:02
  • 1
    @Joseph: You said you moved the operator implementation to a .cpp file. That's well and good. But you still want to have this line in the header file: `bool operator<(const Point &,const Point &);` -- Do you? – Benjamin Lindley Apr 25 '12 at 03:03
  • @BenjaminLindley: Duh, no I did not. Adding that in there seems to have fixed it all! – Joseph Apr 25 '12 at 03:04
  • @Joseph: Another option would be what Michael Anderson said. Keep the definition in the header file, but add `inline` in front of it. – Benjamin Lindley Apr 25 '12 at 03:07