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()null になる可能性があります。

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 に答える