문제

I wanted to make a general function that would search for a class type in a node and returns its address. It is defined below

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

But when I pass a search type like SoCoordinate3::getClassTypeId() and the node to be searched for senode as given below:

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

But the last line is generating a Unhandled Exception Error. Please tell what am I doing wrong here. The last line is valid since the same written inside the scope of the function works but not here.

올바른 솔루션이 없습니다

다른 팁

With this you are standing that mysearch.getPath() could be null:

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

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

but below you are using that without any check:

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

so this can raise an Unhandled Exception.

Another poitn is the line:

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

There is no check about how many points are in the vector, and this as well could cause an exception.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top