2010-03-03 7 views
2

저는 Cocoa 응용 프로그램에서 작은 C++ 라이브러리를 사용하고 있습니다 (atm은 내가하는 방법을 배우는 간단한 예제입니다). (에서Objective-C++ 내에서 C++ 클래스 사용

#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 

그리고 마지막으로 구현 :

다음
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 파일이 다음과 같은

그래서 내가 찾고 네임 스페이스에 작은 C++ 클래스가 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 클래스에서이를 래핑 할 수 있습니다. 그러나 이것은 분명히 그것을하는 가장 편안한 방법은 아닙니다.

답변

6

의 이름을 WindowController.mm으로 바꿉니다.

파일 이름이 .m 인 경우 전체 translational unit은 ++없이 ObjC로 컴파일됩니다. 그러나 test.h에서 C++ 코드를 포함하는 testlib.h을 가져오고 있습니다. 이 C++ 코드는 C에서 유효하지 않으므로 컴파일러 오류가 발생합니다. BTW


,

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

당신은 슈퍼의 init 호출해야합니다 : 테스트를 반환해야

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

속성

@property void* testClass; 

을 *.

@property(readonly,assign) testlib::Test* testClass; 
+0

감사를 구축 히트 [슈퍼 초기화] - 나는 그것을 잊어 버렸습니다. Objective-C를 처음 접했을 때 나는 보통 C++로 프로그램을했다. –

3

파일을 이름을 변경하지 않으려면, 당신은 명시 적으로 파일에 대한 정보 창을 열어 ++ ObjC 같은 "하는 .m"파일을 컴파일 엑스 코드를 알 수 있습니다 (왼쪽에서 파일 이름을 클릭 메인 XCode 윈도우의 창에서 파란색 "정보"버튼을 누릅니다. "파일 형식"을 "sourcecode.cpp.objcpp"로 변경하십시오.

샘플 앱의 경우 이름을 바꾸 겠지만 기존의 코드베이스에서 전체 이름 바꾸기를 사용하지 않으려면 "파일 형식"설정을 사용하는 것이 좋습니다.

+0

문제는 다른 .m 파일에서 test.h 파일을 가져 왔다는 것이다. (구현은 이미 test.mm이라고 불렀다. WindowController.m) - 가끔은 간단하고 분명한 문제에 봉착했습니다. 답변을 찾기에는 너무 멀기 때문입니다 ... 힌트를 보내 주셔서 감사합니다. –

1

은 "다른 이름으로 컴파일 소스"를 아래 설정을 만들 이동 목표 - C++로 변경하고 함께 팁을위한

관련 문제