0

I have a class, the constructor looks like this:

myclass(int=0,string="",int=0,string="",int=0,int=0,
        int=0,int=0,string="",int=0,int=0); 

and a vector with elements of this type

vector<myclass>myvect;

the vector is sorted and I am trying to remove duplicates and this is not working:

std::vector<myclass>::iterator it;
   it=std::unique (myvect.begin(), myvect.end());   
   myvect.resize(std::distance(myvect.begin(),it) );

I get this error

:algorithm(1862): error C2678: binary '==' :
 no operator found which takes a left-hand operand 
of type 'myclass' (or there is no acceptable conversion) 

any idea why? is there any way I can remove duplicates from this vector?

Sonicpath
  • 231
  • 4
  • 16

3 Answers3

3

The std::unique algorithm needs to know how to compare two myclass objects for equality. There are two ways you can do this. The first is to implement myclass::operator==. The second is to pass a binary predicate to std::unique:

std::unique (myvect.begin(), myvect.end(),
             [](const myclass& a, const myclass& b) {
               return /* some expression to test equality */;
             }); 
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

You probably didn't implement myclass::operator==.

Marc Claesen
  • 16,778
  • 6
  • 27
  • 62
1

You need to overload the operator == for your class myclass in order to apply the unique algorithm.

Quoting from std::unique documentation:

The function uses operator== to compare the pairs of elements (or pred, in version (2)).

taocp
  • 23,276
  • 10
  • 49
  • 62