我正在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) {
}
答案 0 :(得分:1)
您必须在MyContact
类中实现比较opeartor才能将其插入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
需要比较运算符才能维护它的内部二进制搜索树。