This is a long shot

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.

Does anyone know how to fix this??
I'm using c++

Thanks

Attached: 1509331832849.png (629x504, 36K)

Other urls found in this thread:

cplusplus.com/reference/vector/vector/pop_back/
twitter.com/SFWRedditImages

Whats a vector? I only know java

i tried initially using erase(), just to be clear.

it's an array that you can initialize objects in.

Post code you idiot

Oh, this really isnt a hard assignment then lol

Java has vectors too, friend.
It's the equivalent of ArrayList but with a different implementation.

dynamic array

Like List?

Nvm yeah i use arraylist every day at work, good stuff

Are you looking through the list type using its own enumerator?

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++) {..}

it's really long
here is the part that matters:


vector cH;
vector tH;
vector mH;
vector m;
vector c;
vector t;


for(i=0;i

what's your major

if you're gonna do deletion and insertion operations over sequences it's better to use linked lists

Yeah if you have a few million items

you're fucking hopeless, give up now

Why are you using at instead of the [] operator?
Also is m.erase(m.at(p)) not working?

I was using an iterator but even if i don't specify the item and just use a push_back AFTER the loop is over it still crashes

>Also is m.erase(m.at(p)) not working?
no
I haven't tried with [], i'll do it now but i don't think it will make any difference

I meant pop_back() not push

Actually i think it should be m.erase(m.begin() +p)) based on what you're doing

cplusplus.com/reference/vector/vector/pop_back/
>If the container is not empty, the function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.

Are you checking if the vec is empty before calling pop_back?

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.

>i can
see*

m.(begin(m) + p) actually

nigga what

nvm i had a meltdown already

wew

>>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