2

私はこのようなクラスシーンを持っています:

class Renderer;

class Scene
{
public:
    Scene(const std::string& sceneName);
    ~Scene();

    void Render(Renderer& renderer);

    Camera& GetSceneCamera() const;
    SceneNode& GetRootNode() const;
    const std::string& GetSceneName() const;


private:
    const std::string mName;
    Camera mSceneCamera;
    SceneNode mRootNode;
};

次に、シーンのベクトル ( vector<Scene>) を取得します。

このシーンのベクトルを反復処理したい文字列が与えられ、シーンの中で名前が見つかった場合は、その名前へのポインターを返します。これは素朴な試みですが、コンパイルエラーが発生しています:

Scene* SceneManager::FindScene(const std::string& sceneName)
{
    return std::find_if(mScenes.begin(), mScenes.end(), boost::bind(&std::string::compare, &sceneName, _1));
}

ブーストは引数の数について不平を言っているので、構文が間違っている必要があります..これを行う正しい方法は何ですか?

編集:No instance of overloaded boost::bind matches the argument list

EDIT2: C++11 ではありません

ありがとう

4

2 に答える 2

4

これを段階的に見てみましょう。

find_if は、ベクトル内の各要素に対して比較関数を呼び出し、比較関数が true を返すと停止します。関数は、パラメーターで呼び出し可能である必要がありconst Scene &ます。

次のように記述できます (このコードはすべてテストされていません)

struct SceneComparatorName {
    SceneComparatorName ( std::string &nameToFind ) : s_ ( nameToFind ) {}
    ~SceneComparatorName () {}
    bool operator () ( const Scene &theScene ) const {
        return theScene.GetSceneName () == s_;
        }
    std::string &s_;
    };

さて、どのようにインラインで記述しますか? boost::bindへの呼び出しがなく、 aとa をGetSceneName比較できないため、の試行は失敗します。Scene &std::string

C++11 の場合

上記の構造体が行うことを正確に行うラムダを書くのは簡単です。

[&sceneName] (const Scene &theScene ) { return theScene.GetSceneName () == sceneName; }

ただし、c++11 は必要ないため、次のように記述する必要があります。

boost::bind ( std::string::operator ==, sceneName, _1.GetSceneName ());

ただし、バインドによって作成されたファンクターが呼び出されるときではなく、バインドの呼び出し内で GetSceneName が呼び出されるため、これは機能しません。

ただし、Boost.Bind はオーバーロードされた演算子をサポートしているため、次のように記述できます。

    boost::bind ( &Scene::GetSceneName, _1 ) == sceneName

そして行われます。詳細については、 http://www.boost.org/doc/libs/1_52_0/libs/bind/bind.html#nested_bindsのドキュメントを参照してください。

于 2012-12-24T01:19:40.700 に答える
0

最短の方法は、おそらく手動ループです。

BOOST_FOREACH(Scene& scene, mScenes) {
    if (scene.GetSceneName() == sceneName) return &scene;
}
return 0;
于 2012-12-24T00:02:04.590 に答える