2014-11-13 3 views
1

C++ 클래스 메서드에서 객관적인 C 메서드를 호출 할 수 있습니까? 나는 이것이 어느 정도까지 대답되었다는 것을 알고 있지만 객관적인 C 인스턴스 변수 (self에 대한 포인터)를 사용하여 메소드를 호출하려고 할 때 '선언되지 않은 식별자 사용'을 얻었으므로 허용 된 응답 중 아무 것도 나를 위해 작동하지 않는 것으로 알고 있습니다. .C++ 클래스 메서드에서 객관적인 C 메서드 호출

@interface RTSPHandler : NSObject { 
    id thisObject; 
} 

도 구현 :

-(int)startRTSP:(NSString *)url { 

    thisObject = self; 
    // start rtsp code 
} 

void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, 
           struct timeval presentationTime, unsigned) { 

    [thisObject receivedRTSPFrame:fReceiveBuffer]; 

}  

-(void)receivedRTSPFrame:(NSMutableData*)data { 

    // decode frame.. 

}   

오류 '에서 thisObject'

+0

귀하의 C++ 파일의 확장자는 .mm입니까? obj-C++ 파일에 obj-c와 C++ 만 혼합 할 수 있습니다. – Fonix

+0

예. 그렇습니다.이 문제를 언급하는 것을 잊었습니다! – Md1079

+0

'RTSPHandler'와'DummySink'의 관계는 무엇입니까? – molbdnilo

답변

1

시도 선언되지 않은 식별자의 사용과 같은 정적 변수 thisObject 선언

static id thisObject; 
@implementation RTSPHandler 
//... 
@end 

UPDATE

아래

확인. 이제 나는 내 대답이 웃기는 것을 본다. 과제를 수행하고 솔루션을보다 적절하게 만들어 봅시다.

별도의 인터페이스와 구현 부분이있는 두 개의 별도 클래스가 있습니다. OCObjectiveClass (Objective-c 클래스) 및 DummySink (C++ 클래스)이라는 objective-c 클래스를 말하십시오. 각 DummySink 인스턴스는 OCObjectiveClass의 개체를 C++ 클래스 멤버로 가져야합니다. 이 DummySink의 인터페이스 부 (a ".H"-extension와도)이다

@interface OCObjectiveClass : NSObject 
    //... 
    - (void)receivedRTSPFrame:(void *)frame; // I don't know what is the frame's type and left it with a simple pointer 
    //... 
@end 

:

이 (a '.H "-extension)와 OCObjectiveClass의 인터페이스 부분

#import "OCObjectiveClass.h" // include objective-c class headers 
class DummySink 
{ 
    OCObjectiveClass *delegate; // reference to some instance 
    //... 
    void AfterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,struct timeval presentationTime, unsigned); 
    //... 
} 

AfterGettingFrame 함수 구현은 DummySink 클래스 구현 부분 (".cpp"확장이 아니고 objective-c 클래스 및 메서드로 작업하려면 ".mm"이어야 함)에 있어야합니다.

void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, 
           struct timeval presentationTime, unsigned) { 

    [delegate receivedRTSPFrame:fReceiveBuffer]; 

} 

delegate 값을 설정해야합니다.

- (void)someMethod 
{ 
    OCObjectiveClass *thisObject; 
    // initialize this object 
    DummySink sink; 
    sink.delegate=thisObject; 
    sink.DoWork(); 
} 
+0

클래스의 인스턴스를 하나만 가질 수 있습니까? 그게 좋은 일인가? – trojanfoe

+0

이것이 효과가있는 이유는 모르지만 해결책이었습니다. 많이 감사드립니다 :) – Md1079

+0

@ Md1079 @ 트로이안 호프입니다. 'thisObject'는 전체 클래스의 유일한 인스턴스입니다. 조심해. – Astoria

관련 문제