Problema semántico de C++, const value_type to pointer*

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;
}

¡gracias por tu tiempo!

preguntado el 03 de mayo de 12 a las 09:05

3 Respuestas

Cambio:

std::vector<b2Fixture> fixtures;

a:

std::vector<b2Fixture *> fixtures;

About the return type you can change it either to void* or std::vector<b2Fixture *> * y 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).

contestado el 03 de mayo de 12 a las 09:05

It's not really clear what you want to do, but the problem is you're telling the compiler that AbstractContactListener::getFixturesOfTypeCollidingWithFixture devolverá un id and you're instead returning an std::vector<b2Fixture>.

From the name of the function, I guess you might want to return a vector, so change the signature to:

std::vector<b2Fixture> AbstractContactListener::getFixturesOfTypeCollidingWithFixture
                                                      (b2Fixture *fix, int type)

You're also pushing pointers in your vector when you should be pushing objects:

fixtures.push_back(*(contact.fixtureB));

contestado el 03 de mayo de 12 a las 09:05

El vector fixtures mantiene b2Fixture instances, but the contact.fixtureA es un b2Fixture*.

Ya sea:

  • dereference it:

    fixtures.push_back(*(contact.fixtureA)); // Same for 'fixtureB'.
    

    o,

  • cambiar el tipo de fixtures:

    std::vector<b2Fixture*> fixtures;
    

There is also a mismatch between the function return type and what is actually being returned. If you want to return fixtures, have the return type match the type of fixtures.

contestado el 03 de mayo de 12 a las 09:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.