1问题: bug提示图下图所示:

  BUG_vector iterator not dereferencable

2. 分析

   前前后后核实程序没有错误,退后根据bug提示信息

 vector<Point>::iterator itPoint = tempSortedList.begin();

while (itPoint->y >= yLine && itPoint->y < yLine+MOVEUNION && itPoint != tempSortedList.end() )   

  在vector的源文件中

  BUG_vector iterator not dereferencable

   提示内容的意思是:“vector迭代器不可以去引用操作”。

  但是在while条件判定中添加了:itPoint != tempSortedList.end(),迭代器的到达末尾的判定。

  唯一可能的原因是,当到达末尾后,外部又从新进入whlie条件判定:

  itPoint->y >= yLine && itPoint->y < yLine+MOVEUNION && itPoint != tempSortedList.end()

  但是首先进行的是:itPoint->y,取引用操作,因为此时迭代器指向了tempSortedList.end(),所以抛出此异常

3. 修改

   首先进行迭代器是否到末尾的判定,然后在进行其他迭代器操作,这是基于如果到达末尾迭代器位置,则不进行下述判定。

while (itPoint != tempSortedList.end() && itPoint->y >= yLine && itPoint->y < yLine+MOVEUNION)

  程序运行结果正常。

  如下写法同样不妥,需要调整:

    while( currentPNode->x < node->x  &&   NULL != currentPNode )

 

4. 心得

  写程序要用心考虑,思维缜密,考虑周全!

  while()条件判断,先进行“成立条件判断”,再进行“假定成立后的条件判断”

 

 

 

 

 

 

 

 

 

 

相关文章: