fspirit
fspirit

Reputation: 2547

How to use Objective-C in C++ header?

I tried to do the following in the A.h file:

#include "Bar.hpp"

#import <Foundation/Foundation.h>

namespace foo 
{
  struct A : Bar::B
  {
    public:

    A() : Bar::B() {}

    id delegate;

    virtual void OnEvent(...);
  };
}

But I get zillion of errors like 'I dont know what NSString is'. How do I do it correctly?

Upvotes: 5

Views: 1349

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

If you would like to use one of your Objective C classes inside your "regular" C++ class (as opposed to Objective C++) you could use the trick described in this article, which boils down to including <objc/objc-runtime.h> instead of <Foundation/Foundation.h>, and using a wrapped struct objc_object in place of a "real" Objective C object.

#ifdef __OBJC__
@class ABCWidget;
#else
typedef struct objc_object ABCWidget;
#endif

namespace abc
{
  class Widget
  {
    ABCWidget* wrapped;
  public:
    Widget();
    ~Widget();
    void Reticulate();
  };
}

Upvotes: 3

Farcaller
Farcaller

Reputation: 3070

You're including it in a .cpp file? Rename it to .mm (that's the correct file extension for Objective-C++).

Upvotes: 5

Related Questions