2

I have a c++ class IPoint and want to use an instance of IPoint as an instance variable for an obj-c class. But when compiling it gives me the error: "expected specifier-qualifier-list before 'Ipoint'" However I have included the desired header that is "IPoint.h".

But when I use the cpp object in my class just by initializing it without making it an instance variable, it works.

There is a specific requirement of the cpp object to be stored as instance variable since it is required further in my project, if there could be a way to make it work like writing a wrapper for the object or anything else. kindly help me out!

2 Answers 2

4

If you want to be able to #import your Objective-C class interface into both Objective-C and Objective-C++ code, you can use an #ifdef to declare the instance variable as void* for the former:

@interface MyClass : NSObject {
    #ifdef __cplusplus
    IPoint *point;
    #else
    void *point;
    #endif
}

Unfortunately, this does mean you'll need to manage the lifetime of the C++ object manually, creating and destroying it with new and delete in your Objective-C++ init and dealloc methods, respectively.

Sign up to request clarification or add additional context in comments.

Comments

1

The header for an Objective-C class with C++ class ivar must be compiled as Objective-C++. Make sure to use .mm extension instead of .m extension in the source code filename, or set the file to compile as Objective-C++ in Xcode.

Comments

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.