views:

291

answers:

3

I have an Objective-C/C++ application which uses functionality that is provided by a C++ library.

One of the C++ classes includes an enum like this:

class TheClass
{
public:
[...]

enum TheEnum
{
    YES,
    NO,
};

[...]
};

Including (using #import -if that matters-) a header file with the above class declaration in an Objective-C/C++ source file (*.mm) will make the compile fail since the preprocessor will replace "YES" by the term "(BOOL) 1" (and likewise "NO" by "(BOOL) 0").

Is there a way to fix that without renaming the values of the enum?

A: 
Meredith L. Patterson
YES and NO are defined macros in Objective-C. You can't use the terms as an identifier.
xtofl
Okay, but in that case, wouldn't reversing the order of declaration still work?
Meredith L. Patterson
#import causes the MIDL compiler to generate a header file containing a C++ representation of the import library, that's #include'd afterwards by the preprocessor.
xtofl
Good to know, thanks for filling me in.
Meredith L. Patterson
@Meredith L. Patterson: you're welcome.
xtofl
+4  A: 

YES and NO are predefined constants in Objective-C, declared in the objc.h header.

You should be able to prevent the preprocessor to expand the "YES" and "NO" macro's. This can be done by locally #undeffing them.

But technically, if you're using a language keyword as an identifier, you can expect trouble. You won't write a class containing a member called MAX_PATH, would you?

xtofl
A: 

Sorry this should be a comment not a reply.

xtofl - #import and MIDL is what happens under Visual C++ on Windows and that is not what is being asked about here. I agree with the rest of your comments

In Objective C #import is roughly the equivalent if C #include and import guards in the include file. See the Objective C FAQ http://thaesofereode.info/clocFAQ/#preproc-import To include C++ headers I would use #include as there are cases where import and include do behave differently.

Mark