0

私は Xcode で cocos2d プロジェクトに取り組んでおり、衝突検出を数週間機能させようとしています。私は、接触リスナーを使用して衝突を検出すると言った Ray Wenderlich チュートリアルを使用しています。ただし、Invalid operands to binary expression ('const MyContact' and 'const MyContact')というエラーが発生します。このエラーを見たことがありません。誰か助けてもらえますか?

#import "MyContactListener.h"

MyContactListener::MyContactListener() : _contacts() {
}

MyContactListener::~MyContactListener() {
}

void MyContactListener::BeginContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.insert(myContact);  <------------//Says "7.In instantiation of member function 'std::set<MyContact, std::less<MyContact>, std::allocator<MyContact> >::insert' requested  here"
}

void MyContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::set<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
    _contacts.erase(pos);
}
}

void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}

void MyContctListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}
4

1 に答える 1

1

MyContactに挿入するには、クラスに比較演算子を実装する必要がありstd::setます。何かのようなもの:

class MyContact
{
...
    bool operator<(const MyContact &other) const
    {
        if (fixtureA == other.fixtureA) //just pointer comparison
            return fixtureB < other.fixtureB;
        return fixtureA < other.fixtureA;
    }
};

比較演算子はstd::set、内部の二分探索ツリーを維持するために必要です。

于 2013-07-05T06:42:00.167 に答える