2012-05-03 15 views
0

私はこのC++メソッドを使ってb2Fixtureインスタンスの配列を返そうとしています。このように定義されているJRContactインスタンスのシリーズ、の繰り返し処理:C++の意味論的な問題、ポインタへのconst value_type *

struct JRContact { 
    b2Fixture *fixtureA; 
    b2Fixture *fixtureB; 
    bool operator==(const JRContact& other) const 
    { 
     return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB); 
    } 
}; 

n.b.コメントでエラーを参照してください、私はC++に見ず知らずの人よ、私はそのコードに;-)

を行っているかもしれない奇妙なものを言及することを躊躇しないで、次は(MacOSの上XCodeのコンパイラ)をコンパイルに失敗します。

id AbstractContactListener::getFixturesOfTypeCollidingWithFixture(b2Fixture *fix, int type){ 

    std::vector<b2Fixture> fixtures; 

    std::vector<JRContact>::iterator ct; 
    JRContact contact; 

    for (ct = _contacts.begin(); ct != _contacts.end(); ct++){ 

     contact = *ct; 

     if ( 
       ((fix == contact.fixtureA) || (fix == contact.fixtureB)) && 
       (contactContainsType(contact, type)) 
      ){ 

      if (fix == contact.fixtureA) { 

       // error: Semantic Issue: Reference to type 'const value_type' (aka 'const b2Fixture') could not bind to an lvalue of type 'b2Fixture *' 

       fixtures.push_back(contact.fixtureB); 
      } 

      else { 

       // error: Semantic Issue: Reference to type 'const value_type' (aka 'const b2Fixture') could not bind to an lvalue of type 'b2Fixture *' 
       fixtures.push_back(contact.fixtureA); 
      } 
     } 
    } 

    // error: Semantic Issue: No viable conversion from 'std::vector<b2Fixture>' to 'id' 
    return fixtures; 
} 

ありがとうございました!

答えて

2

変更:

std::vector<b2Fixture> fixtures; 

へ:戻り値の型について

std::vector<b2Fixture *> fixtures; 

あなたがvoid*またはstd::vector<b2Fixture *> *と使用にいずれかを変更することができます。return &fixtures;

しかし、あなたのベクトルは注意を払いますローカルなので、無効な場所へのポインタを返さないように割り当てます。 (そして、あなたがそれを使用したときに、それを解放することを忘れないでください)。

+0

パーフェクト、ありがとう! – Jem

1

あなたがしたいことは本当にはっきりしませんが、AbstractContactListener::getFixturesOfTypeCollidingWithFixtureidを返し、代わりにstd::vector<b2Fixture>を返すことをコンパイラーに伝えているという問題があります。

関数の名前から、私はあなたがvectorを返すようにしたい、そうに署名を変えるかもしれない推測:あなたがオブジェクトをプッシュしなければならないとき、あなたはまた、あなたのベクトルにポインタをプッシュしている

std::vector<b2Fixture> AbstractContactListener::getFixturesOfTypeCollidingWithFixture 
                 (b2Fixture *fix, int type) 

fixtures.push_back(*(contact.fixtureB)); 
+0

ああ、確かに、それは多くのおかげで、ありがとう! – Jem

1

ベクターfixturesb2Fixtureインスタンスを保持しているが、contact.fixtureAb2Fixture*あります。

いずれか:それは間接参照

  • fixtures.push_back(*(contact.fixtureA)); // Same for 'fixtureB'. 
    

    又は、

  • 変化fixturesの種類:

    std::vector<b2Fixture*> fixtures; 
    

また、関数の戻り値の型と実際に返される値との間に不一致があります。 fixturesを返却する場合は、返品タイプをfixturesのタイプと一致させてください。

+0

こんにちは、ありがとう、それは大きな助けです:) – Jem

関連する問題