MartinG
MartinG

Reputation: 9

C++ class declaration in objective-c header

I want to declare a class c++ style in a objective-c header, but i get an error "error: expected '=', ',', ';', 'asm' or '__ attribute __' before 'CPPClass'"

Here is code from the .h file.

class CPPClass;  
@interface OBJCClass : NSObject  
{  
    CPPClass* m_pCPPObject;  
}  
@end

if i implement it objective-c style @class CPPClass i get an error when defining it, saying that it can't find the interface declaration. Is there anyway of doing this, otherwise, all the objective-c classes that import my header file with the imported c++ header must also be .mm files.

ps. i've have renamed the m file to mm.

Upvotes: 0

Views: 2572

Answers (2)

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

Declare the cpp class only when compiling C++. Use a typedef to void otherwise:

#ifdef __cplusplus
class CPPClass;
#else
typedef void CPPClass;
#endif

This way, non C++ compilation units see the instance variable as a void pointer. Since all pointers are of the same size, the type of the instance variable does not matter.

Upvotes: 3

jer
jer

Reputation: 20236

Rename any files that include it as having .mm extensions. This will tell the compiler to compile with the -ObjC++ flag.

Upvotes: 1

Related Questions