Accessing c++ class from Obj-C .h file
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:
cppclass.h
cppclass.cpp
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 ?
"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:
cppclass.h
Code:
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED
class CppClass
{
public:
void setId(int newId);
int getId();
private:
int myId;
};
#endif // FOO_H_INCLUDEDcppclass.cpp
Code:
#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 ?
From what I know of, .mm is the only option. Problem is that with c++ everything gets compiled as c++.
You could try:
Of course this means no methods.
You could try:
Code:
//c++ File
class MyClass{int x;} A;
//c header
typedef struct {int x;}MyClassAlias;
extern MyClassAlias A;
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.
Yes, I understand about .mm. The problem I'm having is that if I include "cppclass.h" in EAGLView.mm everything works, and I can create instances of that class. Now if I include it in EAGLView.h - it does not compile =(
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Creating a window and accessing OpenGl using C++ | RagingAvatar | 5 | 3,868 |
Nov 5, 2009 10:37 AM Last Post: OneSadCookie |
|
| "Unknown class in nib file" | MacFiend | 1 | 3,057 |
Feb 6, 2007 03:34 PM Last Post: presterjohn |
|
| Accessing iTunes DB through Applescript? | blobbo | 2 | 3,142 |
Dec 30, 2005 06:51 AM Last Post: blobbo |
|

