2013-12-16 16 views
0

我想做一个通用函数来搜索节点中的类类型并返回它的地址。它被定义下面Coin3D中未处理的异常错误(Open Inventor)

SoNode* searchandgive(SoType searchtype, SoNode* searchnode) 
{ 
    SoSearchAction mysearch; 
    mysearch.setType(searchtype); 
    mysearch.setInterest(SoSearchAction::FIRST); 
    mysearch.apply(searchnode); 
    if (mysearch.getPath() == NULL) { 

     std::cout<<"No property of this type was found"; 
    } 

    SoPath* mypath=mysearch.getPath(); 
    return mypath->getTail(); 
} 

但是当我通过像SoCoordinate3 :: getClassTypeId()的搜索类型和节点要搜索senode如下给出:

SoCoordinate3 * mycoords=(SoCoordinate3*) searchandgive(SoCoordinate3::getClassTypeId(),senode); 
const SbVec3f *s=mycoords->point.getValues(0); 
std::cout<<" " <<s->getValue()[25]; // Some point 

但最后一行正在产生未处理的异常错误。请告诉我在这里做错了什么。最后一行是有效的,因为在函数的作用域内写入的内容相同,但不在这里。

+0

你为什么不检查空指针返回? –

回答

0

有了这个,你站在那mysearch.getPath()可能为空:

if (mysearch.getPath() == NULL) { 

     std::cout<<"No property of this type was found"; 
    } 

但低于你使用的是没有任何检查:

SoPath* mypath=mysearch.getPath(); 
    return mypath->getTail(); 

所以这可以引发未处理的异常。

另一个poitn是行:

std::cout<<" " <<s->getValue()[25]; // Some point 

没有关于多少分是矢量,而这也可能会导致异常检查。

+0

我已经缩短了发布的代码。如果我们不考虑最坏的情况,即找到SoCoordinate3,那么解释是什么。我试过了searchandgive函数范围内的代码,它工作正常。此外,mypath.getTail()指向的地址也与mycoords指向的地址相同。 – Utkarsh51

+0

感谢Felice的快速反应。但是我知道getValue [25]存在,我甚至在函数范围内尝试了相同的内容,并得到了正确的答案,但是不在函数的范围之外。我正在使用SoSTLFileKit类导入stl。 – Utkarsh51