矢量迭代器不是dereferencable ....?

时间:2012-02-27 19:14:53

标签: vector iterator

`void calc_distance(vector fingerTips,CvPoint palmCenter,IplImage * source) {     双倍距离= 0;

vector<CvPoint>::iterator p;

if (fingerTips.size() != NULL && fingerTips.size() <= 5 && fingerTips.size()  >= 1)
{   

    if ((fingerTips.size() > 1) || (fingerTips.size() <= 5))
        {

        distance = 0;

        p = fingerTips.begin();

        CvPoint forefinger = *p;

        CvPoint secondfinger;

        for( ;p != fingerTips.end(); )
                    {

                           p++; 

                           secondfinger = *p;

               distance +=  sqrt(double((forefinger.x - secondfinger.x) * 

(forefinger.x - secondfinger.x)+(forefinger.y - secondfinger.y)*(forefinger.y - secondfinger.y)));

                cvLine(source,forefinger,secondfinger,cvScalar(1.0,1.0,1.0),3,8);

                forefinger = secondfinger;

            }
                    }
     }

}

//作为参数我传递了指尖坐标的矢量和手掌的中心以及图像源 但仍然得到错误:“矢量迭代器不是不同的”.... 错误发生在第二次迭代“secondfinger = * p;”线..... 请帮帮我......

在这里,我试图获得每个手指之间的距离,并将每个手指相加 最后的距离......

请帮帮我......

2 个答案:

答案 0 :(得分:1)

您检查p != end,然后P++,然后取消引用。你应该只为循环做一个标准:

for(; p != fingerTips.end(); p++)
{
    //p++ //This is gone now. It's up in the for loop
   ...
}

而不是在for循环中使用p++

答案 1 :(得分:0)

在递增之前,您需要取消引用p;否则,当你到达集合的末尾时,你最终会尝试取消引用fingertips.end(),这是无法解除引用的。

for( ;p != fingerTips.end(); ++p)
{
    secondfinger = *p;

    distance +=  sqrt(double((forefinger.x - secondfinger.x) * (forefinger.x - secondfinger.x) + (forefinger.y - secondfinger.y) * (forefinger.y - secondfinger.y)) );

    cvLine(source,forefinger,secondfinger,cvScalar(1.0,1.0,1.0),3,8);

    forefinger = secondfinger;

}