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

  • 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.
  • On Feb 17, 2008, at 3:49 AM, Leopard x86 wrote:
    >
    > 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...

    It might make the most sense to figure out what those problems are and
    fix them. Often it is a simple matter of needing to include another
    file first.

    >
    >
    > 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.

    Hmm... I'm not sure you understand what extern is for.
    >
    >
    > extern "C" {
    > class foo;
    > };

    Basically, this code tells the compiler to handle the linkage to
    functions and variables in the brackets as though the current language
    is C instead of C++. Of course, there are no functions or variables in
    those brackets, just a forward declaration  of a class. You can drop
    the extern "C" and just forward declare class foo, but that probably
    won't help. If all you ever do in your code is pass the pointer around
    (you never call 'new foo' or access any of the classes members) then
    it might be ok. However, if you actually need to *use* an object of
    class foo, you need the full definition of the class.

    Of course, this is a very non-Cocoa-ish question. Heck, I doubt it is
    even related to Objective-C[++]. You might want to take further
    questions to a more appropriate place (and no, I don't know where that
    place is - perhaps someone else can recommend a list).

    --Brady
  • You may want to read the (recent) "Objective-C++" thread in the archives...

    On 17/02/2008, Leopard x86 <mailinglist.developer...> 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.
    >