Avatar of MBarongan
MBaronganFlag for United States of America asked on

Can't add custom objects to a set

I have a custom class named videoType. I'm trying to add several videoType objects to a set named videoSet like this:

  set<videoType> videoSet;  
  videoType video1("Titanic", 1);
  videoType video2("Star Wars", 2);

When I try to add video1 to the set, I get an error message that points to this line of code in the stl_functions.h file:

/// One of the @link comparison_functors comparison functors@endlink.
  template<typename _Tp>
    struct less : public binary_function<_Tp, _Tp, bool>
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x < __y; }

I overloaded all the comparison operators in the videoType class so that the videoType objects can be sorted. Is there something else I need to do to enable the objects to be inserted into the set?

Attached are my files.
Programming Languages-OtherC++

Avatar of undefined
Last Comment

8/22/2022 - Mon

The attched videoType.h file has an error in the function that overloads the > operator:

bool videoType::operator>(videoType otherVideo)
 return (ID < otherVideo.ID);

I just corrected (ID < otherVideo.ID) to  (ID > otherVideo.ID), but this did  not resolve the issue.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

I focused on less (<) rather than > because set, by default compares using less.

An alternative to your defining operator<() is to define a functor comparator that you associate with your set constructor. See, for example, how struct classcomp is used in the following link:
// constructing sets
#include <iostream>
#include <set>

bool fncomp (int lhs, int rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}

int main ()
  std::set<int> first;                           // empty set of ints

  int myints[]= {10,20,30,40,50};
  std::set<int> second (myints,myints+5);        // range

  std::set<int> third (second);                  // a copy of second

  std::set<int> fourth (second.begin(), second.end());  // iterator ctor.

  std::set<int,classcomp> fifth;                 // class as Compare

  bool(*fn_pt)(int,int) = fncomp;
  std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare

  return 0;

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

Thank you Zoppo and phoffric. Adding const and the '&' symbol fixed the problem.