• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
using C++ class in the Obj-C file (FAILED...)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

using C++ class in the Obj-C file (FAILED...)


  • Subject: using C++ class in the Obj-C file (FAILED...)
  • From: Leopard x86 <email@hidden>
  • Date: Sun, 17 Feb 2008 11:49:32 +0000

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


  • Follow-Ups:
    • Re: using C++ class in the Obj-C file (FAILED...)
      • From: "Herb Petschauer" <email@hidden>
    • Re: using C++ class in the Obj-C file (FAILED...)
      • From: Brady Duga <email@hidden>
  • Prev by Date: Re: MPTask equivalent?
  • Next by Date: Creating and Displaying Bitmap in CGContextRef
  • Previous by thread: Re: MPTask equivalent?
  • Next by thread: Re: using C++ class in the Obj-C file (FAILED...)
  • Index(es):
    • Date
    • Thread