vector.erase doesn´t work

i am working on a program for playing snake and my vector removes the first element just every second passing of the method
maybe anyone has an idea to help me
thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  unsigned tetrissnake::movedown(void)
    {
        if(positiony == 19)
        {

            return 0;
        }
         if(matchfield[(positiony + 1) * 20 + positionx] == "p");
        {
            snakelength.push_back(positiony * 20 + positionx);
        }
        positiony = positiony + 1;
        snakelength.push_back(positiony * 20 + positionx);
        snakelength.erase(snakelength.begin());
        
    }
What do you want to happen?

You're invoking vector::erase(position) , which removes the item at that position. If you want to clear the whole thing, use the two-parameter vector::erase(first, last) method instead. For example, snakelength.erase(snakelength.begin(), snakelength.end()); or simply call snakelength.clear(); You probably just want to call it snake to be less confusing.

Perhaps the erase is working like you intended, but the real problem is something in the method itself? Looks like it's possible for *two* calls to snakelength.push_back(positiony * 20 + positionx); if the if statement results in true.

Edit: by the way, that method signature is a bit strange. Did you mean unsigned int tetrissnake::movedown() ?
Last edited on
i think i found the mistake...
i made an ; at the end of the if- block in the method
so the following block is always running
i cannot believe it
i am sitting here for hours
but you are right i just found it because of your quotation
and sorry for my bad english I´m german
oh, lol, I also missed that semicolon ;D That would do it!
Topic archived. No new replies allowed.