Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ beginner's coding mistake: "Undeclared identifier"?

I'm to use C++ for a very small part of my project. I must be coding something wrong, but my knowledge of C++ is what it is and I can't get around this...

See both the AbstractContactListener.h and .mm files below. The problem is in isFixtureCollidingWithFixtureOfType(...) method, I can't access the _contact vector. What could I be doing wrong here?

header:

struct JRContact {
    b2Fixture *fixtureA;
    b2Fixture *fixtureB;
    bool operator==(const JRContact& other) const
    {
        return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
    }
};

class AbstractContactListener : public b2ContactListener {

    id contactHandler;

public:
    std::vector<JRContact>_contacts;

    AbstractContactListener(id handler);
    ~AbstractContactListener();

    void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type);

    virtual void BeginContact(b2Contact* contact);
    virtual void EndContact(b2Contact* contact);
};

Implementation:

AbstractContactListener::AbstractContactListener(id handler) : _contacts() {
    contactHandler = handler;
}

AbstractContactListener::~AbstractContactListener() {
}

void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type){

    std::vector<JRContact>::iterator ct;

    // Next line is faulty... can't call _contacts.begin()
    // xCode says: "Use of undeclared identifier _contacts"
    ct = _contacts.begin();
}


void AbstractContactListener::BeginContact(b2Contact* contact) {
    // ...
}

void AbstractContactListener::EndContact(b2Contact* contact) {
    // ...
}

Undeclared? Hmm. I thought I was declaring it in the header, right after the "public:" keyword.

What could I be doing wrong here? thanks a lot! J.

like image 365
Jem Avatar asked Jan 27 '26 12:01

Jem


2 Answers

You forget to add the scope of the function. Try:

void AbstractContactListener::isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type){

Why is the error pointing you to that strange place? The compiler sees your function definition and thinks that this is a free function, as there is nothing that indicates otherwise and tries to handle it as such. It fails, because it tries to find the variable in the global scope. This can get even funnier (read: more confusing): Image that this function does not use a class member. It will be simply parsed and compiled as a free function. As soon as your try to call it on an object of that type you will get a linker error.

Also, I cannot see a declaration of the type id which is used in AbstractContactListener but that might just be because the code sample is incomplete.

like image 154
pmr Avatar answered Jan 29 '26 04:01

pmr


You forgot the class name from

void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type)
like image 29
Matti Virkkunen Avatar answered Jan 29 '26 04:01

Matti Virkkunen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!