c++布尔值不同于0或1
C++ Boolean Values Different than 0 or 1
我正在使用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(¤tTime);
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(¤tTime);
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时,它只是一个清除对象使用的内存的方法。-
相关文章:
- 不同于按值传递和常量引用传递的程序集
- 使用 MAKEWORD / MAKEWPARAM 使用布尔值而不是布尔值
- 为什么布尔值不能比作最后一点?
- 为什么这个程序不输出布尔值?
- 错误错误 C2872:"布尔值":kinect.h 的不明确符号
- 元组布尔值不会更改
- 检查字符串中的小写字符或空格.布尔值不起作用
- 为什么使用 int 的转换,而不是布尔值
- lambda 转换为布尔值,而不是推导函数指针类型
- for_each不返回(布尔)值
- 如何接受 0 作为整数而不是布尔值
- Windows HANDLE RAII 管理,如果返回布尔值而不是句柄怎么办?
- 在命令行程序中使用布尔值。找不到定义标志并检查它们是否在参数中的方法
- 是否有任何理由在C 中使用布尔值的枚举,而不是已经存在的布尔
- "if (var)"使用数字转换而不是布尔值
- 在C++中,通过引用传递常量布尔值是不是不好的
- 为什么 isnan、isinf 和 isfinite 返回 int 而不是布尔值
- 如何在 lambda 中返回字符串而不是布尔值
- 为什么我应该使用 1 位位字段而不是布尔值
- c++布尔值不同于0或1