0

I'm working on a cocos2d project in Xcode and I've been trying to get collision detection to work for weeks. I've been using the Ray Wenderlich tutorials which said to use a contact listener to detect collision. However, I get the error Invalid operands to binary expression ('const MyContact' and 'const MyContact'). I have never seen this error, can anyone help with this?

#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) {
}
evanlws
  • 93
  • 1
  • 11

1 Answers1

1

You have to implement comparison opeartor in MyContact class in order to insert it into std::set. Something like:

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

Comparison operator is need by std::set in order to maintain it's internal binary search tree.

Andrew
  • 22,844
  • 11
  • 57
  • 88
  • Thanks! I'm fairly new to this however, so where exactly do I put the comparison operator? I've been looking into it but I can't see where this fits in my code – evanlws Jul 05 '13 at 08:47
  • 1
    @eeveeayen: as I've written, inside MyContact class/struct – Andrew Jul 05 '13 at 08:57