5

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?

3
  • What kind of source are you including it from? It will only work if the language is Objective C++ (not C, Objective C or plain C++). Commented Aug 28, 2012 at 16:13
  • The source, which is shown above, is from an .h file, which has a corresponding .mm implementation. Commented Aug 29, 2012 at 7:16
  • Presumably, you're also including it from other files. Are they all Objective C++ too? If you include it from any source files of other languages, then you'll get errors. Commented Aug 29, 2012 at 8:20

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Or one can set the file type under "Get Info".
No, it's a .h file. That .h file has a corresponding .mm impl.
Make sure that all files that #include/#import this one header are .mm
3

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();
  };
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.