I have a structure with some properties and a set. I have multiple instances of those structs inside another set. When I try to add something to the set inside the struct:
test2.cpp: In function ‘int main()’:
test2.cpp:42:22: error: no matching function for call to ‘std::set<int>::insert(int) const’
(it->ys).insert(2);
I admit it may have something to do with the initialization of the sets. I have a C background and don't fully grasp how object initialization and declaration work in C++. The code:
#include <stdlib.h>
#include <set>
#include <utility>
using namespace std;
struct MyStruct {
long int x;
set<int> ys;
friend bool operator<(MyStruct a, MyStruct b) {
return a.x < b.x;
}
};
set<MyStruct> ms;
MyStruct *new_ms(long int x) {
MyStruct *n= (MyStruct*) malloc(sizeof(MyStruct));
n->x = x;
n->ys = set<int>();
return n;
}
int main() {
pair<set<MyStruct>::iterator,bool> ret;
set<MyStruct>::iterator it;
MyStruct *ms1 = new_ms((long int)5);
ret = ms.insert(*ms1);
if(ret.second == false) {
free(ms1);
}
it = ret.first;
(it->ys).insert(2);
}