2

我在 Cocoa 应用程序中使用了一个小型 C++ 库(atm 只是一个非常简单的示例来了解我应该如何做)。

所以我在命名空间中有一个小的 C++ 类,如下所示:

namespace testlib {
class Test {
public:
    Test(unsigned a);
    Test operator+(const Test& other) const;
    Test operator+(unsigned other) const;
    Test& operator+=(unsigned other);
    Test& operator+=(const Test& other);
    unsigned getValue() const;
private:
    unsigned theValue;
};
}

然后我有一个带有以下代码的 .h 文件:

#import <Foundation/Foundation.h>
#import "testlib.h"

@interface test : NSObject {
     testlib::Test* testClass;
}

- (id)init;
- (id)add: (unsigned)value;
- (unsigned)value;
- (void)dealloc;

@property void* testClass;

@end

最后是一个实现(在一个名为 test.mm 的文件中):

#import "test.h"

@implementation test

@synthesize testClass;

- (id)init {
 testClass = new testlib::Test(0);
 return self;
}

- (void)dealloc {
 delete testClass;
 [super dealloc];
}

- (id)add: (unsigned)value {
 *testClass += value;
 return self;
}

- (unsigned)value {
     return testClass->getValue();
}

当我尝试编译它时,出现以下错误:

CompileC build/testipod.build/Debug-iphonesimulator/testipod.build/Objects-normal/i386/WindowController.o WindowController.m normal i386 objective-c com.apple.compilers.gcc.4_2
 cd /Users/sausalito/eth/testipod
 setenv LANG en_US.US-ASCII
 setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -x objective-c -arch i386 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk -fvisibility=hidden -mmacosx-version-min=10.5 -gdwarf-2 -iquote /Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-generated-files.hmap -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-own-target-headers.hmap -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-all-target-headers.hmap -iquote /Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-project-headers.hmap -F/Users/sausalito/eth/testipod/build/Debug-iphonesimulator -F/Users/sausalito/eth/testipod/../testlib/build -I/Users/sausalito/eth/testipod/build/Debug-iphonesimulator/include -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/DerivedSources/i386 -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/DerivedSources -include /var/folders/4f/4fSYMOmtHHSRoBf+XVFQ+k+++TM/-Caches-/com.apple.Xcode.502/SharedPrecompiledHeaders/testipod_Prefix-cwdputxcxpofoydkulngkdplqxbt/testipod_Prefix.pch -c /Users/sausalito/eth/testipod/WindowController.m -o /Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/Objects-normal/i386/WindowController.o

 In file included from /Users/sausalito/eth/testipod/Classes/test.h:10,
             from /Users/sausalito/eth/testipod/WindowController.m:10:
/Users/sausalito/eth/testipod/Classes/testlib.h:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'testlib'
 In file included from /Users/sausalito/eth/testipod/WindowController.m:10:
/Users/sausalito/eth/testipod/Classes/test.h:13: error: expected specifier-qualifier-list before 'testlib'

/Users/sausalito/eth/testipod/Classes/test.mm:13:0 /Users/sausalito/eth/testipod/Classes/test.mm:13: error: type of property 'testClass' does not match type of ivar 'testClass'

我究竟做错了什么??看起来它使用 C 编译器来编译头文件。

当我将成员声明为 void* 类型然后在实现中使用强制转换时,我可以将其包装在 Objective-C 类中。但这绝对不是最舒服的方式。

4

3 回答 3

6

将您的重命名WindowController.mWindowController.mm.

如果文件名是.m,则整个“翻译单元”将在 ObjC 中编译而没有 ++. 但是,在您的 中test.h,您正在导入testlib.h包含 C++ 代码的文件。这些 C++ 代码在 C 中无效,从而导致编译器错误。


顺便提一句,

- (id)init {
 testClass = new testlib::Test(0);
 return self;
}

你应该调用 super 的 init:

- (id)init {
  if ((self = [super init]))
     testClass = new testlib::Test(0);
  return self;
}

该物业

@property void* testClass;

应该返回一个测试*。

@property(readonly,assign) testlib::Test* testClass;
于 2010-03-03T13:53:33.453 回答
3

如果您不想重命名文件,还可以通过打开文件的信息窗口明确告诉 XCode 将“.m”文件编译为 ObjC++(单击主 XCode 窗口左窗格中的文件名,然后点击蓝色的“信息”按钮)。将“文件类型”更改为“sourcecode.cpp.objcpp”。

For a sample app, I would just rename the file, but if you've got an existing codebase that you don't want to do a wholesale rename on, the "file type" setting can be handy.

于 2010-03-03T14:13:35.410 回答
1

go to build settings under the "Compile Source As" change to Objective-c++, and hit build

于 2011-02-10T19:56:19.897 回答