tags:

views:

34

answers:

2

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.

A: 

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

jer
I was afraid of that, it is more than a couple of files that includes that file and was hoping that there was some way of going around that :(
MartinG
A: 

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.

Nikolai Ruhe
Is there a reason for this downvote?
Nikolai Ruhe