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);
 
  videoSet.insert(video1);

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>
    {
      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.
videoType.h
main.cpp
Programming Languages-OtherC++

Avatar of undefined
Last Comment
MBarongan
Avatar of MBarongan
MBarongan
Flag of United States of America image

ASKER

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.
SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of phoffric
phoffric

I focused on less (<) rather than > because set, by default compares using less.
    http://www.cplusplus.com/reference/set/set/
Avatar of phoffric
phoffric

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:
    http://www.cplusplus.com/reference/set/set/set/
// 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

Avatar of MBarongan
MBarongan
Flag of United States of America image

ASKER

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

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo