I’m trying to get this C++ method to return an array of b2Fixture instances. It iterates of a series of JRContact instances, which are defined like:
struct JRContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const JRContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
n.b. I’m a total stranger to C++, don’t hesitate to mention weird things I might have done in that code 😉
The following fails to compile (XCode compiler on MacOS), see errors in comments:
id AbstractContactListener::getFixturesOfTypeCollidingWithFixture(b2Fixture *fix, int type){
std::vector<b2Fixture> fixtures;
std::vector<JRContact>::iterator ct;
JRContact contact;
for (ct = _contacts.begin(); ct != _contacts.end(); ct++){
contact = *ct;
if (
( (fix == contact.fixtureA) || (fix == contact.fixtureB) ) &&
( contactContainsType(contact, type) )
){
if (fix == contact.fixtureA) {
// error: Semantic Issue: Reference to type 'const value_type' (aka 'const b2Fixture') could not bind to an lvalue of type 'b2Fixture *'
fixtures.push_back(contact.fixtureB);
}
else {
// error: Semantic Issue: Reference to type 'const value_type' (aka 'const b2Fixture') could not bind to an lvalue of type 'b2Fixture *'
fixtures.push_back(contact.fixtureA);
}
}
}
// error: Semantic Issue: No viable conversion from 'std::vector<b2Fixture>' to 'id'
return fixtures;
}
Thanks for your time!
Change :
to :
About the return type you can change it either to
void*orstd::vector<b2Fixture *> *and use :return &fixtures;But pay attention the your vector is local so allocate it for not returning a pointer to an invalid location. (And ofcourse remember to free it when you done using it).