calling an Objective C method from C++
calling an Objective C method from C++
- Subject: calling an Objective C method from C++
- From: Paul Cezanne <email@hidden>
- Date: Thu, 27 Feb 2003 11:20:26 -0500
I'm trying to call an Objective-C method from a C++ object. Converting the C++ object to Obj-C is not an option, this is cross platform code.
Here is what I've done so far:
I define a C++ wrapper class with a pointer to the Obj-C object:
class CFooWrapper
{
public:
void myTest(int j, , char *str);
void setMyController(id c);
private:
id my_Controller;
};
Then implement this class, calling objc_send to send the onJunk message to the receiver:
void CFooWrapper:: myTest(int j, char *str)
{
printf("j=%d, str=%s\n", j, str);
SEL aSel = @selector(onJunk:label:);
objc_msgSend(my_Controller, aSel, j, titleStr);
}
In the Objective-C class header, I declare the OnJunk class, a pointer to the C++ wrapper class, and a method that will be eventually called by the C++ wrapper class.
@interface CFooController : NSObject
{
CFooWrapper *m_CFooWrapper;
}
- (void) OnJunk:(int) j label: (char *) str;
@end
I then implement the OnJunk method
- (void) OnJunk:(int) j label:( char *)str
{
printf("OnJunk %d, (%s)\n", j, str);
}
and finally instaniate the C++ wrapper class and pass the Objective-C object to it so that it can call it.
m_CFooWrapper = new CFooWrapper;
m_CFooWrapper->setMyController(self);
First, thanks for reading this far! My problem is that this doesn't work! It actually doesn't even compile.
...../CFooWrapper.h:31: `id' was not declared in this scope
...../CFooWrapper.h:31: parse error before `)' token
...../CFooWrapper.h:34: 'id' is used as a type, but is not defined as a type.
At first I thought that I wasn't compiling with the Objective-C compiler, so I changed it to be a .m file. That didn't help.
Line 31 is void setMyController(id c);
Line 34 is id my_Controller;
It seems that when compling this file, the compiler doesn't know what id is. How can I make it know that? Thanks.
--
--
Paul Cezanne
Senior Member of Technical Staff
Sequel Imaging
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.