-1

I got:

"object.h"

namespace objectNS
{
    class object
    {
    public:
        int m_number;
        bool operator== (const object& object) const;
        bool operator< (const object& object) const;
    };
    class compare
    {
    public:
        bool operator ()(const object*, const object*) const;
    };
}

"object.cpp"

#include "object.h"
typedef objectNS::object OBJECT;
bool OBJECT::operator== (const object &object) const
{
    return this->m_number == object.m_number;
}

bool OBJECT::operator< (const object &object) const
{
    return this->m_number < object.m_number;
}

bool objectNS::compare::operator() (const object* obj1, const object* obj2) const
{
    return obj1->m_number > obj2->m_number;
}

I got some function which works with objects,

void f(void)
{
    set<Compound_object*> detectedObjects;
    set<Compound_object*> deleteFromTrackedObjects;
    set_difference(detectedObjects.begin(), detectedObjects.end(), this->m_trackedObjects.begin(), this->m_trackedObjects.end(), inserter(addToTrackedObjects, addToTrackedObjects.end()));
}

Problem description: I implemented operator< and operator== by which the comparision of two instances is performed by m_number, but when the sets detectedObjects and trackedObjects contain elements with the same m_number, the std::set_difference returns all elements instead of an empty result set as expected.

I even have tried to give a functional object compare to the set as template argument, but as a result I got a lot of compiler errors about = and != not being properly defined.

I wonder what is the problem?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
spin_eight
  • 3,925
  • 10
  • 39
  • 61
  • Please post code that we can compile (`m_number` vs `m_numberOfObject`, `this` in a non-member function), but keep it minimal (what's `compare` for?). Indent properly. – avakar Aug 24 '12 at 09:11
  • 2
    There may some hard to understand typedefs involved, but from your declarations, it seems your sets contain _pointers_ to the objects, rather than the objects themselves. In that case, `operator<` defined as part of the object class won't take effect. – jogojapan Aug 24 '12 at 09:13
  • @ jogojapan, Yes thanks, sounds reasonable, I didn`t paid attention to it. So I need to define proper class as comparator and state it as argument for set? But what is the reason that I am getting those "=", "!=" not defining while trying to provide my compare class as argument to the set? – spin_eight Aug 24 '12 at 09:22

1 Answers1

2

I can't see a good reason why you're storing pointers to objects instead of the objects themselves.

To use a comparator, you need to make sure that you're providing the same comparator type to all of your set instances and to set_difference:

set<Compound_object*, compare> m_trackedObjects;

set<Compound_object*, compare> detectedObjects;
set<Compound_object*, compare> deleteFromTrackedObjects;
set_difference(detectedObjects.begin(), detectedObjects.end(),
    this->m_trackedObjects.begin(), this->m_trackedObjects.end(),
    inserter(addToTrackedObjects, addToTrackedObjects.end()),
    compare());
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • ecatmur, thank you very much for detailed and clever explanation, now code compiles and works as expected! Issue was: I included comp funct object as argument in set, but I forgot to include it to algorithm set_difference as argument. So again thanks and have a nice day! – spin_eight Aug 24 '12 at 09:42