Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;不同于0或1的布尔值_C++ - Fatal编程技术网

C++ C++;不同于0或1的布尔值

C++ C++;不同于0或1的布尔值,c++,C++,我正在使用PCL和OSG库编写一个简单的代码,但我在对象的布尔值上遇到了一个奇怪的行为 以下是我的类定义: class BulletCallback : public osg::NodeCallback { public: bool found_cube; BulletCallback() { found_cube = false; } virtual void operator()(osg::Node* nod

我正在使用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;
                        }
                }

}
类BulletCallback:public osg::NodeCallback { 公众: 布尔发现了一个立方体; BulletCallback(){ 发现_cube=false; } 虚拟void操作符()(osg::Node*Node,osg::NodeVisitor*nv); }; 类BulletTransfAndCallBack { 公众: int启动; BulletCallback*updateCallBack\u btc; osg::参考ptr bulletTransf; BulletTransfAndCallBack(BulletCallback*回调,osg::ref_ptr transf){ updateCallBack_btc=回调; bulletTransf=transf; 时间-当前时间; 时间(¤tTime); 开始=当前时间; } }; std::listBulletTransfAndCallBackList; int main(int argsc,字符**argsv){ std::list::迭代器i=BulletTransfAndCallBackList.begin(); while(i!=BulletTransfAndCallBackList.end()) { bool-removed=false; bool found_c=(*i)->updateCallBack_btc->found_cube; //这个变量“found_c”的值有123265…等等,值大于1,我不明白,它应该是1或0 问题解决了

问题是这个电话:

node->removeUpdateCallback(updateCallBack_btc_this);
它会从场景中移除updateCallBack调用,此外,它会清理updateCallBack_btc_使用的内存,我的布尔变量就在其中


这个方法是自动清除对象,C++不应该这样做。这就是为什么很多人花了几个小时来寻找bug。就像我……当没有bug时,它只是一个清除对象使用的内存的方法。--/p>你必须在某处有不明确的行为。尽量把代码减少到一个更小的、自包含的示例中。我添加了与UpdateCallback方法对应的代码,方法“operator”在BulletCallback类中…我没有更多的代码与布尔变量混淆..我没有更多涉及此布尔变量的代码。它不会出现在设置布尔值的代码中。它将出现在不应该写入的内存上进行内存复制的代码中。为什么不能显示SSCE?代码太多了。您支持“我想把它简化成一个更小的独立的例子。你的样本既不独立也不小。这个词很简单,我认为它的意思和你想象的不一样,@DarkLink”

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);