Compile Error on using STL set...

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
hyperion
New poster
Posts: 8
Joined: Sun Aug 07, 2005 2:21 pm

Compile Error on using STL set...

Post by hyperion »

I don't know why this gets compile error, I found out that OJ gives compile error just when I use find function on a class templated set like the code below, can anyone help me out how can I use the find function on set...

Code: Select all

#include <set>
#include <iostream>

using namespace std;

class Test {
  public :
    int x, y;
};
class comp {
  public:
    int operator () (Test a, Test b)
    {
      if (a.x < b.x || (a.x == b.x && a.y < b.y)) return 1;
      return 0;
    }
};

int main()
{
  set<Test, comp> s;
  Test temp;

  temp.x = 1; temp.y = 2;
  s.insert(temp);
  temp.x = 2; temp.y = 2;
  s.insert(temp);
  temp.x = 2; temp.y = 1;
  s.insert(temp);

  temp.x = 1;  temp.y = 2;
  set<Test, comp>::iterator i = s.find(temp);
  if ( i != s.end() ) cout << "Yes\n"; else cout << "No\n";
  return 0;
}
Thanks alot in advance..[/code]

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof »

I changed your comparison function to a more correct one:

Code: Select all

class comp {
  public:
    bool operator () (const Test &a, const Test &b) const
    {
      if (a.x < b.x || (a.x == b.x && a.y < b.y)) return true;
      return false;
    }
};
now it compiles under all versions of g++ I found

hyperion
New poster
Posts: 8
Joined: Sun Aug 07, 2005 2:21 pm

Post by hyperion »

Thanks alot misof, you are a real guru
:D

Post Reply

Return to “C++”