我有一小段代碼,它包含一個用於二維頂點的類矢量(Vector2)和一個向Vector2類型對象提供參數的模板類DynamicLine矢量。爲什麼我的指針失效,如果我不使用vector.reserve()?
std::vector<Vector2> m_coords;
std::vector<DynamicLine<Vector2>> m_lines;
Vector2 *lineVec = NULL;
bool draw = true;
case DrawingWidgetState::ADD_VERTEX_SELECTED:{
for(auto it = m_coords.begin(); it != m_coords.end(); it++)
if(it->distanceFrom(Vector2(event->x(), event->y())) < 20 && !m_coords.empty()){
draw = false;
break;
}
if(draw){
if(event->x() < m_mainWindow->width() - 10 && event->x() >= 10
&& event->y() < m_mainWindow->height() && event->y() >= 10) {
m_coords.push_back(Vector2(event->x(), event->y()));
update();
}
}
break;
}
case DrawingWidgetState::ADD_LINE_SELECTED:{
for(auto it = m_coords.begin(); it != m_coords.end(); it++)
if(it->distanceFrom(Vector2(event->x(), event->y())) < 10){
if(!i){
lineVec = &(*it);
i++;
}
else{
m_lines.push_back(DynamicLine<Vector2>(lineVec, &(*it)));
i = 0;
}
update();
break;
}
break;
}
問題是未來。例如,如果我添加兩個頂點和它們之間的一條線,那麼一切正常。但是,如果我現在添加另一個頂點(m_coords.push_back(Vector2(event-> x(),event-> y()));),則線條dissaperas和m_lines矢量中的值將更改爲非常隨機的大數字。我讀了一些關於指針失效的信息。如果你將新的值推向矢量,那麼最終會有一些擴展。我也使用vector :: reserve修復了我的問題,但由於我確定我沒有這樣做,任何人都可以請解釋我應該如何使用vector :: reserve以及爲什麼我需要這樣做?
1)修復你的縮進。 2)創建[mcve]。 3)嘗試使用調試器來遍歷代碼,同時在每個步驟檢查變量的值。以該順序。 –