Accessing c++ class from Obj-C .h file

Posts: 4
Joined: 2009.06
Post: #1
Hello. I am new here, and just started learning Obj-C. I do have a lot of experience programming in C++ and C# though. Now the problem I am facing is that I want to write most of my classes in C++ rather Obj-C and use them inside Obj-C. I have found alot of information on ths matter and in most cases one just includes "cppclass.h" inside a ".mm" file and and just uses the class. This works just fine as ".mm" compiles nicely as Obj-C++, however when one has class definition and class body in seperate files as in ".h" and ".mm" it becomes harder to use C++ class, as inclusion of "cppclass.h" inside "objc.h" results in the following error:

"error:expected '=', ',',';','asm' or '__attribute__' before 'GoaEngine'

now including "cppclass.h" streight inside ".mm" file compiles just fine. So how do I use C++ class inside a complex Obj-C++ class that is broken into ".h" and ".mm"?

Here is a code snippets just in case if I've done something stupid:

class CppClass
        void setId(int newId);
        int getId();
        int myId;

#endif // FOO_H_INCLUDED

#include "cppclass.h"
void CppClass::setId(int newId)
    myId = newId;
int CppClass::getId()
    return myId;

Now I have EAGLView.h and EAGLView.m (which I renamed to .mm") created by OpenGL ES Template for iPhone SDK. Just writing #include "cppclass.h" inside EAGLView.h brings up an error. =(

So how do I use C++ class inside Obj-C ?
Quote this message in a reply
Posts: 227
Joined: 2008.08
Post: #2
From what I know of, .mm is the only option. Problem is that with c++ everything gets compiled as c++.

You could try:
//c++ File
class MyClass{int x;} A;
//c header
typedef struct {int x;}MyClassAlias;
extern MyClassAlias A;
Of course this means no methods.
Quote this message in a reply
Posts: 869
Joined: 2003.01
Post: #3
All ObjC modules that use C++ headers need to be compiled as ObjC++, aka all modules that include a C++ header must be .mm files.
Quote this message in a reply
Posts: 4
Joined: 2009.06
Post: #4
Yes, I understand about .mm. The problem I'm having is that if I include "cppclass.h" in everything works, and I can create instances of that class. Now if I include it in EAGLView.h - it does not compile =(
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Creating a window and accessing OpenGl using C++ RagingAvatar 5 5,349 Nov 5, 2009 10:37 AM
Last Post: OneSadCookie
  "Unknown class in nib file" MacFiend 1 3,965 Feb 6, 2007 03:34 PM
Last Post: presterjohn
  Accessing iTunes DB through Applescript? blobbo 2 4,152 Dec 30, 2005 06:51 AM
Last Post: blobbo