2013-07-05 5 views
0

Xcode에서 cocos2d 프로젝트를 진행하고 있으며 충돌 감지가 몇 주 동안 작동하도록 노력했습니다. 나는 충돌 리커버리 (contact listener)를 사용하여 충돌을 감지했다는 Ray Wenderlich 자습서를 사용 해왔다. 그러나 오류가 발생했습니다 이진 표현식에 대한 잘못된 피연산자 ('const MyContact'및 'const MyContact'). 나는이 오류를 본 적이 없으며, 누구도이 문제를 도와 줄 수 있습니까?box2d 연락처 리스너의 의미 문제

#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) { 
} 

답변

1

당신은 std::set에 삽입하기 위해 MyContact 클래스 비교 opeartor를 구현해야합니다. 같은 뭔가 :

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가 필요합니다.

+0

감사! 나는 이것에 상당히 새롭다, 그래서 정확하게 비교 연산자를 어디에 두는가? 내가 찾고있어하지만 내 코드에 맞는이 볼 수 없습니다 – evanlws

+1

@eeveeayen : 내가 쓴 것처럼, MyContact 클래스 내부/구조체 – Andrew