views:

46

answers:

1

Hi all! i'd like to have a function in api style. But implementation must be on Objective-C lang. So i've read some information and decided to do following - to mix objective-C with C++. And have problem to call an objC method in C++ class. Thats my example:

//MYClass.h :

class CClass
{
private:
 id fileName;
 BOOL rez;
public: 
 bool download(char* initial_file_name)
 {
  fileName = [[NSString alloc] initWithUTF8String:initial_file_name];
  // here I'd like to call my obj-c method
  ObjCClass c = [[ObjCClass alloc] init] 
  rez = [c writeFile:fileName];
  if (rez == YES)
   return true;
  return false;
 }
};

@interface ObjCClass: NSObject
{
     CClass *cClass;
}

    - (BOOL) writeFile:(NSString *)fileName;
@end

something like this..

A: 

The line where you want to create your instance of the Objective-C class is wrong, there is a '*' and a ';' missing, it should look like this:

ObjCClass *c = [[ObjCClass alloc] init];

You also need to add a forward declaration for your Objective-C class in front of the C++ class:

@class ObjCClass;

Those changes should make the code compilable, but you will still get warnings. To fix these you’ll have to move the Objective-C class to the beginning and forward declare the C++ class before.

Sven
Great! Thanks a lot! %)
Pavel
I got another question :) I'm compiling this code as a .dylib file. Made an app, include MYClass.h in project and have an error: objc_class_name referenced from Literal-pointer@__OBJC@__cls_refs@ in main.o symbol not found - how to solve this problem? It go's when i include my .h file
Pavel