Can't find standard C++ includes when using C++ class in Cocoa project

☆樱花仙子☆ 提交于 2019-12-05 02:58:58

The problem with your CppWrapper class is that it doesn't present a pure Objective-C interface. In your CppWrapper.h file, you're importing the C++ class's header file, which means that any Objective-C class that imports the wrapper class will need to be compiled as Objective-C++, including your TestAppDelegate.

Instead, you'd need to do something like this to completely hide the C++ within the CppWrapper.mm file:

//  CppWrapper.h
#import <Foundation/Foundation.h>

@interface CppWrapper : NSObject {
    void *myCppClass;
}
- (void)doSomethingWithCppClass;
@end


//  CppWrapper.mm
#import "CppWrapper.h"
#import "CppClass.h"

@implementation CppWrapper

- (id)init {
    self = [super init];
    if (self) {
        myCppClass = new CppClass;
    }    
    return self;
}

- (void)dealloc {
    delete myCppClass;
    [super dealloc];
}

- (void)doSomethingWithCppClass {
   static_cast<CppClass *>(myCppClass)->DoSomething();
}

@end

Personally, I would

#include "CppClass.h"

instead of importing it.

That's probably not your problem though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!