0

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
2
  • 8
    Do you really need that many pointers?
    – GManNickG
    Sep 27, 2011 at 18:39
  • That's a lot of pointers. Did you come from Java perchance? Sep 27, 2011 at 19:47

1 Answer 1

3

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.

2
  • 1
    ahh, I got it finally, thanks so much! And i will look into the local variable issue
    – Stephen D
    Sep 27, 2011 at 18:47
  • @StephenD: Good. Must look into the local variable issues. Sep 27, 2011 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.