0

I'm trying to do something similiar to an instance which hinerit from a @Protocol in Objective-C, so I would like to have a @Protocol with some declared methods, and an instance where I can override and use my methods.

With C++ for now I did a class with virtual methods liket his:

class MyClassA
{

public:
~MyClassA(){};
virtual void doSomething(argument *) = 0;

}

and a class (the one I want to instantiate and use)

class MyClassB : public MyClassA
{

public:
~MyClassB() {};
void doSomething(argument *a);

}

in the implementation file of both these classes I've implemented doSomething() like this:

void doSomething(argument *a)
{
 // yep do something
}

then in AnotherObject (which is a subclass of CCNode) I want to create an ivar of MyClassB and here's where I'm failing at, I tried to put a simple pointer to the class like:

MyClassB *myB; 

and then use it like:

myB->doSomething;

but obviously it's going to crash with bad_access

I tried to instantiate the object before using

MyClassB *myB = new MyClassB;

or

MyClassB *myB = MyClassB::create();

but got a linker error whatever I try. The error says:

Undefined symbols for architecture i386:
"MyClassB::doSomething(argument*)", referenced from:
AnotherObject::init() in AnotherObject.o
"vtable for MyClassB", referenced from:
AnotherObject::init() in AnotherObject.o

I'm missing something to achieve the same as

id<Protocol>object; 

with objective-C which I know better than C++

ps. I've decided to write Cocos2d even if this is a C++ question because of the init and create_func method that probably are in some way related

Adarkuccio
  • 973
  • 4
  • 12
  • 24
  • You need to provide an implementation for `MyClassB::doSomething(argument*)`. In the code above you merely showed its declaration and it seems you either haven't implemented it or you didn't add the file where it is implemented to your build. BTW, if your class has any `virtual` function you are best off to make the destructor `virtual`, too! – Dietmar Kühl Nov 10 '13 at 12:44
  • try omitting the declaration of doSomething in the subclass B – CodeSmile Nov 10 '13 at 12:44
  • possible duplicate of [How do you declare an interface in C++?](http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c) outside of objc protocols are usually called an"interface" – CodeSmile Nov 10 '13 at 12:45

2 Answers2

2

Implement MyClassB::doSomething(). You declared it, you used it, therefore you have to implement it.

You have to implement doSomething() in the implementation file like

void MyClassB::doSomething(argument *a)
{
 // yep do something
}

not like void doSomething(argument *a).

Oswald
  • 31,254
  • 3
  • 43
  • 68
  • ok sorry this was probably so obvious that I missed to tell you but, I've implemented it in the MyClassA (parent) and then in the MyClassB :P I'm going to update my question – Adarkuccio Nov 10 '13 at 13:04
  • I think the mistake is where I initialize the object of that class which hinerit from my interface, here's what I'm missing about it, can someone please show me an example? the way I did it is surely wrong – Adarkuccio Nov 10 '13 at 13:09
  • seems like I didn't forgot to write them, but I forgot to write them well, C++ is new for me so sorry for this stupid mistake ! – Adarkuccio Nov 10 '13 at 13:21
1

in the implementation file of both these classes I've implemented doSomething() like this:

void doSomething(argument *a)
{
 // yep do something
}

So you've defined a non-member function with the same name. You wanted to define the member function:

void MyClassB::doSomething(argument *a)
     ^^^^^^^^^^
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • YES, that was, I wrote the rest of the files correctly and now this one I missed MyClassB:: before the method name, I'm used to use (and think with) Objective-C so sometimes I get stuck on things like that with C++... shame on me ! thank you anyway – Adarkuccio Nov 10 '13 at 13:21
  • Just maybe you would rather use Objective-C and port to Android via Apportable? Saves you all this C++ trouble. ;) – CodeSmile Nov 10 '13 at 14:46