So, it seems that a lot of people new to C++ have problems with vectors, as their is a ton of questions about vector. I am not really new to C++ but I have never had this problem. Anyway, so my problem is with vector pointers. I am making a video game with a vector that holds the objects. It holds a reference to the objects so it can do collision response inside the collision function. To load the vector I used to do something like this:
Object a = Object(12, 135, 123, 124);
collision_rects.push_back(&a);
It worked perfectly like this, however I didn't want to type each individual object, so I made a function that read it from a text file, like this.
void get_from_file()
{
//Blah blah read numbers from file.
//then I would get the numbers and make a object from them.
Object a = Object(numbers from file);
collision_rects.push_back(&a);
}
But doing this didn't work! I fall through the floor! If I take the same data and load it the first way it works fine, but when I do it from a function in a text file it doesn't. I then realized that maybe it was because the Object went out of scope. So I tried making an object global then doing the same in the function, which worked. This is essentially the same as the first option but in a function. So I made two vectors, on the same scope so that the original object would not go out of scope like this.
std::vector<Object> objects;
void get_from_file()
{
//blah blah
objects[i] = Object(nums from file); //reading from numbers in a for loop
collision_rects.push_back(&objects[i]); //This throws weird
This didn't work either.(By the way, this is not even close to actual code, just a example of what the code does basically. What am I doing wrong? Thanks. EDIT So I don't know if I explained it perfectly, but I don't think its the problem that its pointing to a deleted object. That certainly would make it not work, however it is also another reason I believe.
player->cHandler.objects.push_back(&uno);
player->cHandler.objects.push_back(&dos);
player->cHandler.objects.push_back(&tres);
player->cHandler.objects.push_back(&quatro);
player->cHandler.objects.push_back(&cinco);
player->cHandler.objects.push_back(&seis);
player->cHandler.objects.push_back(&a8);
player->cHandler.objects.push_back(&a9);
player->cHandler.objects.push_back(&a10);
player->cHandler.objects.push_back(&a11);
player->cHandler.objects.push_back(&a12);
player->cHandler.objects.push_back(&a13);
player->cHandler.objects.push_back(&a14);
player->cHandler.objects.push_back(&a15);
player->cHandler.objects.push_back(&a16);
player->cHandler.objects.push_back(&a17);
player->cHandler.objects.push_back(&a18);
player->cHandler.objects.push_back(&a19);
player->cHandler.objects.push_back(&a20);
Now this is my actual code that works.
However when I do it like this, it doesn't work.
objs.push_back(uno);
objs.push_back(dos);
objs.push_back(tres);
objs.push_back(quatro);
objs.push_back(cinco);
objs.push_back(seis);
objs.push_back(a8);
objs.push_back(a9);
objs.push_back(a10);
objs.push_back(a11);
objs.push_back(a12);
objs.push_back(a13);
objs.push_back(a14);
objs.push_back(a15);
objs.push_back(a16);
objs.push_back(a17);
objs.push_back(a18);
objs.push_back(a19);
objs.push_back(a20);
for (int i = 0; i < objs.size(); i++)
{
//player->cHandler.objects.push_back(&objs[i]);
}
This is not working, and I don't know why.
collision_rects.push_back(a);Will work in all cases, provided you change your vector to not store pointers.