我想做一个通用的函数来搜索节点中的类类型并返回它的地址。它的定义如下
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但是最后一行生成了一个未处理的异常错误。请告诉我这里哪里做错了。最后一行是有效的,因为在函数范围内编写的相同代码有效,但在这里不起作用。
发布于 2013-12-16 16:24:25
这样,您就可以断定mysearch.getPath()可能为空:
if (mysearch.getPath() == NULL) {
std::cout<<"No property of this type was found";
}但在下面,您使用的是未经任何检查的代码:
SoPath* mypath=mysearch.getPath();
return mypath->getTail();所以这会引发一个未处理的异常。
另一个问题是这一行:
std::cout<<" " <<s->getValue()[25]; // Some point没有检查向量中有多少个点,这也可能导致异常。
https://stackoverflow.com/questions/20606143
复制相似问题