BrendanS
BrendanS

Reputation: 331

Having trouble instantiating C++ class from objective C

I'm mixing C++ with ObjectiveC for a Cocos2d (objective-C) and Box2D (C++) project.

I have the following C++ class:

class ActorListener : public b2ContactListener
{ 
    public :
    const b2Body* Owner;

    ActorListener(const b2Body* owner);
    ~ActorListener();

    virtual void BeginContact(b2Contact* contact);
    virtual void EndContact(b2Contact* contact);
    virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);    
    virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};

Which I try to initialise with:

(in header)

ActorListener* Listener;

(in mm file)

Listener = new ActorListener(Body); 

I get the error:

Undefined symbols for architecture i386:
  "vtable for ActorListener", referenced from:
      ActorListener::ActorListener(b2Body const*)in ActorListener.o
      ActorListener::ActorListener(b2Body const*)in ActorListener.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

Upvotes: 0

Views: 830

Answers (2)

rob mayoff
rob mayoff

Reputation: 385500

The vtable is emitted in the same file as the first virtual function of the class. In this case, ~ActorListener() is the first virtual method, because b2ContactListener::~b2ContactListener() is virtual. Did you remember to define ActorListener::~ActorListener() somewhere?

Upvotes: 4

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81674

The trick is to get XCode to use g++ to do the linking. You can force this either by including a C++ source file on the link line -- even a fake, essentially empty one -- or (I think) by explicitly linking with /usr/lib/libstdc++.6.dylib .

Upvotes: 0

Related Questions