我正在寫一個基於Allegro的蛇遊戲,並且有一個包含蛇部分座標的向量的問題。向量迭代器錯誤
這是刪除蛇的最後一個元素並添加一個新元素的函數。 以前我宣佈vector <int> snake_x ; vector <int> snake_y;
,我也推回了向量中所需的數字。
void moove_snake(int direction){
switch (direction){
case UP:
{
vector <int>::iterator xIter;
vector <int>::iterator yIter;
snake_x.erase(snake_x.begin());
snake_y.erase(snake_y.begin());
xIter=snake_x.end();
yIter=snake_y.end();
snake_x.push_back(*xIter);
snake_y.push_back(*yIter-20);
draw();
al_flip_display();
al_rest(0.4);
break;
}
case DOWN:
{
vector <int>::iterator xIter;
vector <int>::iterator yIter;
snake_x.erase(snake_x.begin());
snake_y.erase(snake_y.begin());
xIter=snake_x.end();
yIter=snake_y.end();
snake_x.push_back(*xIter);
snake_y.push_back(*yIter+20);
draw();
al_flip_display();
al_rest(0.4);
break;
}
case LEFT:
{
vector <int>::iterator xIter;
vector <int>::iterator yIter;
snake_x.erase(snake_x.begin());
snake_y.erase(snake_y.begin());
xIter=snake_x.end();
yIter=snake_y.end();
snake_x.push_back(*xIter-20);
snake_y.push_back(*yIter);
draw();
al_flip_display();
al_rest(0.4);
break;
}
case RIGHT:
{
vector <int>::iterator xIter;
vector <int>::iterator yIter;
snake_x.erase(snake_x.begin());
snake_y.erase(snake_y.begin());
xIter=snake_x.end();
yIter=snake_y.end();
snake_x.push_back(*xIter+20);
snake_y.push_back(*yIter);
draw();
al_flip_display();
al_rest(0.4);
break;
}
}
}
當我運行它會打開一個錯誤消息框,說矢量迭代器不dereferencable的exe文件。這是什麼意思 ?