Re: using C++ class in the Obj-C file (FAILED...)
Re: using C++ class in the Obj-C file (FAILED...)
- Subject: Re: using C++ class in the Obj-C file (FAILED...)
- From: "Herb Petschauer" <email@hidden>
- Date: Wed, 20 Feb 2008 12:00:03 -0800
You may want to read the (recent) "Objective-C++" thread in the archives...
On 17/02/2008, Leopard x86 <email@hidden> wrote:
> Hi everyone.
> I want to writing an application which uses some UNIX library
> implemented by C++ language.
> Obviously, we should use the objective-C++ to use the C++ class in the
> objective-c file.
>
> Hmm, because there is a ready-made class, assuming it is named as
> "foo". Then we have foo.h/foo.cpp in our project.
>
> The include method is forbidden, for if I included the foo.h, the
> previous files included by foo.h throwed out lots lots of compile
> error. Very horrible...
>
> Then I want to use a trick to solve this: because the UNIX library has
> been compiled into a dylib file, I can use the C++ keyword "extern" to
> let the compiler think the class has been defined. When the code
> compiled, the compiler will automatically find the symbol defined in
> the dylib file and solved the linker.
>
> then my code seems as follows:
> //myObjCClass.h
>
> @#import <Cocoa/Cocoa.h>
> @interface ObjCppClass : NSObject {
> }
> @end
>
> //myObjCClass.mm
> #import "myObjCClass.h"
> extern "C" {
> class foo;
> };
> @implementation ObjCppClass
> -(void) awakeFromNib{
> NSLog(@"This is in the AwakeFromNib");
> foo* _newClass = new foo();
> }
>
> @end
> The following is the C++ class
> //foo.h
> #include <BlahBlahBlah.h>
> #include <Carbon/Carbon.h>
> class foo{
> private:
> int x;
> public:
> void setX(int para);
> int getX();
> };
> //foo.cpp
> #include "foo.h"
> void CppClass::setX(int para){
> x = para;
> }
> int CppClass::getX(){
> return x;
> }
>
> Xcode hates these code and tells me:
> error: invalid use of undefined type 'struct foo'
> error: forward declaration of 'struct foo'
>
> Can anyone tell me how to solve this problem? Any help or suggestion
> is greatly appreciated .Thank you very much in advanced.
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden