c++布尔值不同于0或1

C++ Boolean Values Different than 0 or 1

本文关键字:不同于 布尔值 c++      更新时间:2023-10-16

我正在使用PCL和OSG库做一个简单的代码,但是我在对象的布尔值上得到了一个奇怪的行为。

这是我的类定义:

class BulletCallback : public osg::NodeCallback
{
public:
        bool found_cube;
    BulletCallback() {
                found_cube = false;
        }
    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
};

class BulletTransfAndCallBack 
{
    public:
        int start;
        BulletCallback* updateCallBack_btc;
        osg::ref_ptr<osg::PositionAttitudeTransform> bulletTransf;
        BulletTransfAndCallBack(BulletCallback* callback, osg::ref_ptr<osg::PositionAttitudeTransform> transf ){
            updateCallBack_btc = callback;
            bulletTransf = transf;
            time_t currentTime;
            time(&currentTime);
            start = currentTime;
        }
};
std::list< BulletTransfAndCallBack* > BulletTransfAndCallBackList;

int main(int argsc, char** argsv){
std::list< BulletTransfAndCallBack* >::iterator i = BulletTransfAndCallBackList.begin();
                while (i != BulletTransfAndCallBackList.end())
                {

                        bool removed = false;

                        bool found_c = (*i)->updateCallBack_btc->found_cube;
   //this variable "found_c" has values like 123, 265... and so on, values larger than 1, I don't understand, it was supposed to be 1 or 0
                        cout<<"Found_Cube? ["<<distance(BulletTransfAndCallBackList.begin(), i)<<"] "<<found_c<<endl;
                        if(found_c == true ){
                                time_t currentTime;                     
                                time(&currentTime);
                                int msec = currentTime - ((*i)->start);

                                if(msec > 10){

                                    camera2->removeChild((*i)->bulletTransf);
                                    i = BulletTransfAndCallBackList.erase(i);
                                    removed = true;
                                }

                            }
                        if(removed == false){
                                ++i;
                        }
                }
}

在我的Main方法中使用的变量"found_c"的值像123,265…等等,大于1的值,我不明白,应该是1或者0

但是在类BulletCallback的方法"operator"中,我只看到1或0的值。

有人能帮帮我吗?这是什么?

提前感谢!

编辑:值开始改变,当我添加一个新的bullettransferandcallback到列表,但我使用一个新的BulletCallback…下面是代码:

        mtx.lock();
        BulletCallback* bulletCB = new BulletCallback();
        BulletTransfAndCallBack* btc = new BulletTransfAndCallBack(bulletCB, bulletTransf );
        BulletTransfAndCallBackList.push_back( btc );
        static_cast<osg::Node*>(bulletTransf)->setUpdateCallback( bulletCB );
        mtx.unlock();

EDIT2:所以我已经展示了两个类定义,Main方法…当我添加一个新项目符号并附加UpdateCallback…

时的代码片段

剩下的另一段代码是:

BulletCallback的void方法"operator",在这里:

void BulletCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
        osg::PositionAttitudeTransform* bulletTransf = static_cast<osg::PositionAttitudeTransform*>(node);
        bool btc_found = false;
        BulletCallback* updateCallBack_btc_this;
        BulletTransfAndCallBack* btc; 
        mtx.lock();
        std::list< BulletTransfAndCallBack* >::const_iterator iterator;
            for (iterator = BulletTransfAndCallBackList.begin(); iterator != BulletTransfAndCallBackList.end() && btc_found == false; ++iterator) {
                    if((*iterator)->bulletTransf == bulletTransf ){
                        btc_found = true;
                        updateCallBack_btc_this = (*iterator)->updateCallBack_btc;
                        btc = (*iterator);
                    }
            }
        mtx.unlock();

        mtx2.lock();
        osg::Vec3 v = bulletTransf->getPosition();
    pcl::PointXYZRGBA ballNeighborPoint;
        //verificar se bateu em algum cubo virtual
        if(updateCallBack_btc_this->found_cube == false){
            osg::ref_ptr<osg::PositionAttitudeTransform> cubeTransf;
            osg::Vec3 cubePos;
            std::list< osg::ref_ptr<osg::PositionAttitudeTransform> >::const_iterator iterator;
            for (iterator = cubesList.begin(); iterator != cubesList.end() && updateCallBack_btc_this->found_cube == false; ++iterator) {
                    cubePos = (*iterator)->getPosition();
                    if(abs(v.x()-cubePos.x()) < 5 && abs(v.y()-cubePos.y()) < 5 && abs(v.z()-cubePos.z()) < 5){
                        updateCallBack_btc_this->found_cube = true;                 
                        camera2->removeChild( (*iterator) );
                        cubeTransf = (*iterator);
                        cout << "Cubo destruído! Boa!" << endl;
                    }
            }
            if(cubeTransf != NULL)
                cubesList.remove(cubeTransf);
            if(updateCallBack_btc_this->found_cube == true)
                checkWinning(); 

        }

    std::vector<int> search_indexes;
    std::vector<float> search_radiuses;
    ballNeighborPoint.x = -v.x()/100.f;
    ballNeighborPoint.z = - (v.y()-20)/100.0f;
    ballNeighborPoint.y = -v.z()/100.0f;
    kdtree->radiusSearch (ballNeighborPoint, 0.05, search_indexes, search_radiuses);
    if (search_indexes.size() == 0 && updateCallBack_btc_this->found_cube == false){
            v.y() -= 1;
          bulletTransf->setPosition(v);
    }else{
                ballNeighborPoint.x = -v.x()/100.f;
                ballNeighborPoint.z = -v.y()/100.0f;
                ballNeighborPoint.y = -v.z()/100.0f;

                kdtree->radiusSearch (ballNeighborPoint, 0.05, search_indexes, search_radiuses);
                if (search_indexes.size() == 0){
                    v.z() -= 1;
                      bulletTransf->setPosition(v);
                }else{
                        node->removeUpdateCallback(updateCallBack_btc_this); 
                        //BulletTransfAndCallBackList.remove(btc);
                        //camera2->removeChild(node);
                }
        }
        if(v.y() < -300){
            node->removeUpdateCallback(updateCallBack_btc_this); 
            //BulletTransfAndCallBackList.remove(btc);
            //camera2->removeChild(node);
        }
        mtx2.unlock();
        //cout << "T: " << BulletTransfAndCallBackList.size() << endl;
        cout << "CCC: " << updateCallBack_btc_this->found_cube << endl;
}

我没有更多的代码涉及这个布尔变量…

问题解决了!

问题是这个调用:

node->removeUpdateCallback(updateCallBack_btc_this);

它从被调用的场景中删除了updateCallBack,除此之外,它清理了updateCallBack_btc_this使用的内存,我的布尔变量在里面。

这个方法是自动清理对象,c++不应该这样做。这就是为什么很多人要花几个小时去找bug。像我一样. .当没有BUG时,它只是一个清除对象使用的内存的方法。-