Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:42:12+00:00 2026-06-10T05:42:12+00:00

I got: object.h namespace objectNS { class object { public: int m_number; bool operator==

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:42:13+00:00Added an answer on June 10, 2026 at 5:42 am

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the class object for an enum (I have a Class<? extends Enum>
I've got these two methods in 1 class (Class CokeMachine): public void MoneyButtonEvents() {
ive got an object that looks like this with print_r(): SimpleDOM Object ( [0]
I got an object (called tempEnemy) which is flying around and shooting. The problem
I've got an object containing dates in the format YYYY-MM-DD . I'm extracting the
I've got an object with functions that throw errors, myObj = { ini:function(){ this.f();
I want to invoke the main method which is static. I got the object
I got a business object bounds to a form (each property is bound to
I've got a complex object which is being managed by the LCDS DataServices data
I've got a value object, which stores info for example amount. The getAmount() getter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.