开发者

Inserting complex values to a map in C++

开发者 https://www.devze.com 2023-04-07 20:47 出处:网络
I am having trouble inserting data into this map.I honestly can not figure out the way to do this, but the last line of the code I gave is the part that I need fixed.

I am having trouble inserting data into this map. I honestly can not figure out the way to do this, but the last line of the code I gave is the part that I need fixed.

map<string, vector<vector<Obj*>* >* > the_map;
vector<vector<Obj*> *>*  vectors = new vector<vector<Obj*> *>;开发者_运维知识库
vector<Obj*> Obj_vector;
vectors->push_back(&Obj_vector);                                                    
the_map.insert(make_pair(string("field1", &vectors)); //error on this line only


Try this:

 the_map.insert(make_pair(string("field1"),  vectors)); 
                       //you forgot this ^  ^
                       //                   |
                       //                   & is not needed here 

By the way, I suspect the usage of so many pointers in your code, and especially these two lines:

vector<Obj*> Obj_vector;  //this is local variable
vectors->push_back(&Obj_vector); //inserting address of the local variable

Inserting address of a local variable into vector?

Beware that the local variable wouldn't exist after it goes out of scope, which in turn, means that the address which you just inserted into the vector, points to the destroyed object, and using it would invoke undefined behaviour.

0

精彩评论

暂无评论...
验证码 换一张
取 消