我正在使用Xcode进行cocos2d项目,并且一直在尝试使冲突检测工作数周。我一直在使用Ray Wenderlich教程,该教程说使用接触侦听器来检测碰撞。但是,我收到了对二进制表达式无效的操作数错误(“ const MyContact”和“ 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) {
}
最佳答案
您必须在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
需要比较运算符才能维护其内部二进制搜索树。