I have multiset < Class1 > myset;
so I create a new object: Class1* c1 = new Class1();
I was expecting to be able to myset.insert(c1) or myset.insert(new Class1()); but none of them work.
class Class1{
int time;
public:
CLass1(int t) : time(t) {}
bool operator<(Class1 &c2) {return time < c2.time;}
}
How is inserting objects different from inserting integers? I was able to insert ints.
In your definition,
mysetholdsClass1object, whilec1is a pointer toClass1object. So that’s the type problem.Either you use
mysetto hold pointer to objects —multiset<Class1 *> myset, or copy the newly created object intomyset—myset.insert(*c1); delete c1;. Note that container requires object must be copyable and assignable, and should be comparable by implementingoperator<.