指针列表c ++ opencv属性意外更改

时间:2014-01-10 23:05:05

标签: list pointers

我想保存一个在opencv中跟踪对象的点列表

我利用这个清单

list<list<OrganismPosition*>> trackList;

我插入一个列表和en元素:

list<OrganismPosition*> sublist;
trackList.push_back(sublist);
list<list<OrganismPosition*>>::iterator itList=trackList.end();
itList--;
OrganismPosition* pos=new OrganismPosition();
//in this method I create a new point 
pos->setCenter(&box.center);//Here the point pos->center is (640,550)
itList->push_front(pos);//Error here the point pos->center has value (-103412,-102342)

错误是当我在列表中添加对象时,属性“center”的值随随机值而变化。可能是什么问题以及如何解决这个问题?我已经读过列表调用类的复制构造函数,所以我已经实现了这个来复制正确的属性值,但仍然无法正常工作。我非常感谢任何帮助。感谢

我的班级OrganismPosition:

class OrganismPosition
{
    CvPoint2D32f* center;//center point of organisme tracked
    CvPoint2D32f* head;//head point of organisme tracked
    CvPoint2D32f* queue;//first point of organisme tracked
    CvPoint2D32f* skel1;//second point of organisme tracked
    CvPoint2D32f* skel2;//third point of organisme tracked
    CvPoint2D32f* skel3;//fourth point of organisme tracked
    CvPoint2D32f* skel4;//fiveth point of organisme tracked

public:
    OrganismPosition(void);
    ~OrganismPosition(void);
    OrganismPosition( const OrganismPosition& other );
    void setCenter(CvPoint2D32f* center);
    CvPoint2D32f* getCenter();
    void setHead(CvPoint2D32f* head);
    CvPoint2D32f* getHead();
    void setQueue(CvPoint2D32f* queue);
    CvPoint2D32f* getQueue();
    void setSkel(CvPoint2D32f* skel1,
                 CvPoint2D32f* skel2,
                 CvPoint2D32f* skel3,
                 CvPoint2D32f* skel4);
    void getSkel(CvPoint2D32f* skel1,
                 CvPoint2D32f* skel2,
                 CvPoint2D32f* skel3,
                 CvPoint2D32f* skel4);
};

Class OrganismPosition.cpp:

OrganismPosition::OrganismPosition(void)
{
}


OrganismPosition::~OrganismPosition(void)
{
}
OrganismPosition::OrganismPosition( const OrganismPosition& other )
  {
      OrganismPosition::center->x=other.center->x;
      OrganismPosition::center->y=other.center->y;
      OrganismPosition::head->x=other.head->x;
      OrganismPosition::head->y=other.head->y;


}


    void OrganismPosition::setCenter(CvPoint2D32f* center){
        CvPoint2D32f newcenter=cvPoint2D32f(center->x,center->y);
        this->center=&newcenter;
    }
    CvPoint2D32f* OrganismPosition::getCenter(){
        return this->center;
    }
    void OrganismPosition::setHead(CvPoint2D32f* head){
        CvPoint2D32f newhead=cvPoint2D32f(head->x,head->y);
        this->head=&newhead;
    }
    CvPoint2D32f* OrganismPosition::getHead(){
        return this->head;
    }
    void OrganismPosition::setQueue(CvPoint2D32f* queue){
        CvPoint2D32f newqueue=cvPoint2D32f(queue->x,queue->y);
        this->queue=&newqueue;
    }
    CvPoint2D32f* OrganismPosition::getQueue(){
        return this->queue;
    }
    void OrganismPosition::setSkel(CvPoint2D32f* skel1,
                 CvPoint2D32f* skel2,
                 CvPoint2D32f* skel3,
                 CvPoint2D32f* skel4){
        CvPoint2D32f newskel1=cvPoint2D32f(skel1->x,skel1->y);
        this->skel1=&newskel1;
        CvPoint2D32f newskel2=cvPoint2D32f(skel2->x,skel2->y);
        this->skel2=&newskel2;
        CvPoint2D32f newskel3=cvPoint2D32f(skel3->x,skel3->y);
        this->skel3=&newskel3;
        CvPoint2D32f newskel4=cvPoint2D32f(skel4->x,skel4->y);
        this->skel4=&newskel4;
    }
    void OrganismPosition::getSkel(CvPoint2D32f* skel1,
                CvPoint2D32f* skel2,
                CvPoint2D32f* skel3,
                CvPoint2D32f* skel4){
                skel1=this->skel1;
                skel2=this->skel2;
                skel3=this->skel3;
                skel4=this->skel4;
    }

1 个答案:

答案 0 :(得分:0)

几个错误。首先,我们解决pos->center问题。

void OrganismPosition::setCenter(CvPoint2D32f* center){
    CvPoint2D32f newcenter=cvPoint2D32f(center->x,center->y);
    // you've created a local 2f object that resides on the stack. 
    //this will be destroyed when the function returns.
    this->center=&newcenter;
    // what you're left with is a stack pointer to garbage data.
}

你想要的是在堆上分配内存。

void OrganismPosition::setCenter(const CvPoint2D32f* center){
    if (center)
        delete center; // free memory that you used up before.
    this->center= new cvPoint2D32f(center->x,center->y);
}

现在,为什么要首先考虑所有这个指针头痛?只需在CvPoint2D32f课程中使用OrganismPosition成员即可。除非你绝对需要,否则不要创建指针。