Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown type name 'class' when mixing Objective-C and C++

This is driving me crazy.

I have a C++ class declaration in foo.h file.

class foo // error marks are here
{
};

When I #include/#import this header in my fooUser.mm file, it compiles. When I do #include/#import it in my fooUser.h file it doesn't, and the compiler errors are.

Unknown type name 'class'; did you mean 'Class'?
Excpected ';' after top level declarator.

I'm using XCode 4.2, LLVM compiler 3.0,... should this be important.

like image 598
Merlevede Avatar asked Mar 21 '23 10:03

Merlevede


1 Answers

As you said in a comment, "fooUser.h" is included from non-C++ files as well, and that causes the compiler error. Header files are not compiled separately, but as part of the "compilation unit" from which they are included.

You should try to separate the C++ declarations into a header file that is included only from (Objective-)C++ files.

As a workaround, you could also protect the C++ declarations by

#ifdef __cplusplus
// …
#endif
like image 185
Martin R Avatar answered Apr 01 '23 04:04

Martin R