I’m happily using C++ objects in both, objective C++ (.mm) header and implementation files as well as call into Objective C(++) classes from my C++ implementation files, but every attempt to use Objective C classes in the C++ header file results in compiler errors.
Stuff like this class declaration fails to compile:
class CQuartzTextRenderer {
public:
CQuartzTextRenderer();
virtual ~CQuartzTextRenderer();
virtual void Render(CDrawingOptions &inDrwOpt);
private:
NSFont* CreateNSFontForFontName(string &fontName, int fontSize);
};
no matter what (AppKit/..) includes I’ve tried.
Any ideas?
You can’t have an Objective-C class inside a header file that is included by a
.cppsimply because XCode will use the standard g++ compiler and treat the file as plain C++.To fix these errors (and be allowed to use Objective-C classes in header files) you must make sure that header file is included ONLY by
.mmfiles, not in any.cpp.Just an addition: it may happen that you actually have a
foobar.hcoupled withfoobar.mm. This is correct and allows you to have Objective-C in the header file but if that file is included somewhere else by a.cppor by another.hwhich is included by a.cppthen you’ll get compilation errors. Mind that every unit is compiled independently from others.