views:

346

answers:

2

I've got a project that is primarily in C++, but I'm trying to link in a Objective-C++ library. I have a header that looks something like:

CPlus.h:

#import "OBJCObject.h"

class CPlus {
  OBJCObject *someObj;

};

CPlus.mm:

CPlus::CPlus() {
  someObj = [[OBJCObject alloc] init];
}

When I import the Objective-C++ header into my code I end up with thousands of errors from inside the iPhone SDK. It seems that something is treating one language as if it were another. Sorry if this description is poor, I'm new to this, and am somewhat confused.

Can you include Objective-C / Objective-C++ types in C++ classes? Is there something special you need to do to include the headers for the other types?

+2  A: 

Are you #importing CPlus.h from an Objective-C (.m) file? If so, it will not understand the C++ class since it is being compiled with C semantics, and is not Objective-C++ aware. The .m compiler will see class and not know what to do.

You can include Objective-C objects in C++ class definitions, and vice versa, as long as the source file is .mm.

Jason
A: 

The following builds okay with the IPhone SDK. Make sure to declare the c++ constructor, and to implement it in an .mm file.

OBJCObject.h

#import <Foundation/Foundation.h>

@interface OBJCObject : NSObject {
}

@end

OBJCObject.m

#import "OBJCObject.h"

@implementation OBJCObject

@end

CPlus.h

#import "OBJCObject.h"

class CPlus {
    OBJCObject *someObj;
    CPlus();
};

CPlus.mm

#include "CPlus.h"

CPlus::CPlus() {
    someObj = [[OBJCObject alloc] init];
}
Oren Trutner