0

我想创建一个通用函数来搜索节点中的类类型并返回其地址。它定义如下

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

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

4

1 回答 1

0

有了这个,你站着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

没有检查向量中有多少点,这也可能导致异常。

于 2013-12-16T08:24:24.793 回答