1

Is it possible to call an objective C method from a C++ class method? I am aware this has been answered to an extent but none of the accepted answers appear to be working for me as I get 'use of undeclared identifier' when trying to use the objective C instance variable (a pointer to self) to call the method.

@interface RTSPHandler : NSObject {   
    id thisObject;
}

implimentation:

-(int)startRTSP:(NSString *)url {

    thisObject = self;
    //  start rtsp code  
}

void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
                              struct timeval presentationTime, unsigned ) {

    [thisObject receivedRTSPFrame:fReceiveBuffer];

}    

-(void)receivedRTSPFrame:(NSMutableData* )data {

    // decode frame..

}        

error: use of undeclared identifier 'thisObject'

6
  • does your c++ file have a .mm extension? you can only mix obj-c and c++ in an obj-c++ file Commented Nov 13, 2014 at 11:56
  • yes it does.. I forgot to mention this sorry! Commented Nov 13, 2014 at 11:57
  • What's the relationship between RTSPHandler and DummySink? Commented Nov 13, 2014 at 11:59
  • RTSPHandler contains reference to C++ libraries for rtsp handling. The dummySink class is subclassed from those libraries and called when a frame is received. From here I have my own decoder written in Objective C so it's just a case of calling that method with the data. Commented Nov 13, 2014 at 12:07
  • I am probably going about this the wrong way. Would I be correct in thinking this is a scoping issue.. A class in C++ can only have access to variables set in its creation not from an outside environment such as the objective C class implementation. Commented Nov 13, 2014 at 12:15

1 Answer 1

1

Try to declare thisObject as a static variable like below

static id thisObject;
@implementation RTSPHandler
//...
@end

UPDATE

Ok. Now I see my answer is hilarious. Let's get through the task and make the solution more appropriate.

There will be two separate classes with separated interface and implementation parts. Say the objective-c class named OCObjectiveClass (Objective-c class) and DummySink (C++ class). Each DummySink instance must have an object of OCObjectiveClass as a c++ class member.

This is interface part of OCObjectiveClass (with a ".h"-extension):

@interface OCObjectiveClass : NSObject
  //...
  - (void)receivedRTSPFrame:(void *)frame; // I don't know what is the frame's type and left it with a simple pointer
  //...
@end

This is the interface part of DummySink (with a ".h"-extension too):

#import "OCObjectiveClass.h" // include objective-c class headers
class DummySink
{
  OCObjectiveClass *delegate; // reference to some instance
  //...
  void AfterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,struct timeval presentationTime, unsigned);
  //...
}

AfterGettingFrame function realization must be in DummySink class implementation part (not ".cpp" extension, it must be ".mm" to work with objective-c classes and methods).

void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
                              struct timeval presentationTime, unsigned ) {

    [delegate receivedRTSPFrame:fReceiveBuffer];

} 

Don't forget to set the delegate value.

- (void)someMethod
{
  OCObjectiveClass *thisObject;
  // initialize this object
  DummySink sink;
  sink.delegate=thisObject;
  sink.DoWork();
}
Sign up to request clarification or add additional context in comments.

3 Comments

So you can only have one instance of the class? Is that a good thing?
This was the solution, although I am not sure why it works. Much appreciated :)
@Md1079 @trojanfoe is right. thisObject is the only instance for the whole class. Be careful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.