c++iosobjective-cmixing

error: Use of undeclared identifier 'self' c++-objective c mixing


I'm currently finishing my game for iOS using ue4. I'm using some specific iOS Code and try to call Methods using the 'self' variable

void Aaccessactorios::Shareoption() {
    [self Sharebutton];//ios                 
}

the Code is within a prepocessor tag #if PLATFORM_IOS and #endif

I think that I've declared the self variable at the start of my cpp file:

-(id)init
{
    self = [super init];

    return self;
}

Other Forums Mention that Unreal engine compiles all the cpp .mm at the end, so I don't think that's the issue.

the self variable is used all around the objective c part, so it is crucial to my Code. How do I mix the Code properly and get my self variable to be declared?

Thank you

edit: this is the part i try to recreate https://answers.unrealengine.com/questions/422323/callback-from-objective-c-back-to-the-c-layer.html


Solution

  • I mix C++ and Obj-C all the time. An Obj-C class instance is not the same as a C++ instance. A C++ class instance has this. There is no self. Obj-C class instance has self. You can read more about it here:

    Difference b/w Objective C's self and C++'s this?

    Assuming from what you have written, you have a C++ class Aaccessactorios. It is not clear who has defined Sharebutton. If it is in the C++ class, then you would call it as

    this->Sharebutton();

    or simply

    Sharebutton();

    If Sharebutton belongs to an Obj-C class, then you need to have the C++ either have a pointer to the Obj-C class instance, or have it passed into the function. An example of the latter would be:

    void Aaccessactorios::Shareoption(ObjCClassObj *obj) {
        [obj Sharebutton];//ios                 
    }
    

    In that code reference link, you can see they actually do the former.

     class IOSMediaPlayerController
     {
     public:
         IOSMediaPlayerController();
         ~IOSMediaPlayerController();
         void startPlayback();
         void stopPlayback();
         void callbackFunc();
    
         #if PLATFORM_IOS
         IOSMediaPlayerControllerObj *playerObj;
         #endif    
     };
    

    Here you can see that playerObj is a reference (pointer) to the Obj-C object. This created in the constructor with this

     IOSMediaPlayerController::IOSMediaPlayerController()
     {
         #if PLATFORM_IOS
         playerObj = [[IOSMediaPlayerControllerObj alloc] init];
         #endif
     }
    

    And then gets invoked by

     void IOSMediaPlayerController::startPlayback()
     {
         #if PLATFORM_IOS
         [playerObj startPlayback];
         #endif
     }
    

    Note the example code does not call self, it calls the constructor created instance playerObj.