This is a long shot. I have a project that i have to finish in 2 hours but i'm stuck on an problem so if anyone can help i appreciate.
I have a loop that is suppose to add and delete objects from a vector but everytime i try to delete an element from the vector it generates a error Abort trap 6. I can't use pop_back() or even clear() without causing the program to crash.
i tried initially using erase(), just to be clear.
Nolan Jackson
it's an array that you can initialize objects in.
Asher Thomas
Post code you idiot
Elijah Sanders
Oh, this really isnt a hard assignment then lol
Brayden Rogers
Java has vectors too, friend. It's the equivalent of ArrayList but with a different implementation.
Levi Collins
dynamic array
Aiden Lee
Like List?
Julian Perry
Nvm yeah i use arraylist every day at work, good stuff
Grayson Phillips
Are you looking through the list type using its own enumerator?
Brody Hall
how do you loop over it, using index or iterator ? Each time an element is deleted or added the memory may be reallocated, for this reason you have to use the index based approach.
Also, each time you delete an element decrease the index by one, otherwise you'll skip the next item.
Be sure to use an int to store the index and not an unsigned type, otherwise if you happen to delete the first item and subtract one your index value will be -1, which make the unsigned variable behave in a way you don't want it do do.
basically for(int i = 0; i < int(vec.size(); i++) {..}
Are you checking if the vec is empty before calling pop_back?
Luke Martinez
yes, that's what i did.
it's not empty, i can used push_back() to fill the vector in the beggining of the code and i can before it crashes that the vector is not empity.
Jackson Kelly
>i can see*
Charles Watson
m.(begin(m) + p) actually
Justin Adams
nigga what
Sebastian Anderson
nvm i had a meltdown already
Caleb Miller
wew
John Bailey
>>for(p=0;p { > for(q=0;q { > if((m.at(p).getPos_x() == m.at(q).getPos_x()) && (m.at(p).getPos_y() == m.at(q).getPos_y()) && (p != q)) > { > //?????????????? (The m.at(p) is the item that's supposed to be deleted) > } > } > } when an element gets deleted from m its size will be reduced so you're probably accessing an index thats out of bounds when elements get deleted