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
MBaronganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MBaronganAuthor Commented:
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.
0
ZoppoCommented:
Hi MBarongan,

IMO the problem is you don't use const in your videoType::operator> so the above shown operator()(const _Tp& __x, const _Tp& __y) const cannot call it.

Try to change your function to look like this:
bool videoType::operator>(const videoType& otherVideo) const
{
 return (ID > otherVideo.ID);
}

Open in new window

(In addition to the above mentioned I changed the param to be a reference since it's better for performance)

Hope that helps,

ZOPPO
0
phoffricCommented:
I ran your program in VS 2010, and based on the error, I changed one function to make it compile/build:

bool operator<(const videoType& otherVideo) const;

bool videoType::operator<(const videoType&  otherVideo) const
{
    return (ID < otherVideo.ID);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

phoffricCommented:
I focused on less (<) rather than > because set, by default compares using less.
    http://www.cplusplus.com/reference/set/set/
0
phoffricCommented:
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

0
MBaronganAuthor Commented:
Thank you Zoppo and phoffric. Adding const and the '&' symbol fixed the problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.