2011-07-20 63 views
1

轉換無效我運行在.mm文件下面的代碼,我得到的錯誤:從常量無效*錯誤

Invalid conversion from 'const void*' to 'const __CFData*' 

我需要在.mm運行代碼。如果我更改爲.m它不會抱怨。爲什麼它的行爲如此?我編譯到iPhone

CFSocketNativeHandle native; 
CFDataRef nativeProp = CFReadStreamCopyProperty(theReadStream, kCFStreamPropertySocketNativeHandle); 

if(nativeProp == NULL) 
{ 
    if (errPtr) *errPtr = [self getStreamError]; 
    return NO; 
} 

CFIndex nativePropLen = CFDataGetLength(nativeProp); 
CFIndex nativeLen = (CFIndex)sizeof(native); 

CFIndex len = MIN(nativePropLen, nativeLen); 

CFDataGetBytes(nativeProp, CFRangeMake(0, len), (UInt8 *)&native); 
CFRelease(nativeProp); 

CFSocketRef theSocket = CFSocketCreateWithNative(kCFAllocatorDefault, native, 0, NULL, NULL); 
if(theSocket == NULL) 
{ 
    if (errPtr) *errPtr = [self getSocketError]; 
    return NO; 
} 
+2

指出錯誤發生的位置通常很有幫助。 –

回答

5

CFReadStreamCopyProperty()返回CFTypeRef,這僅僅是一個const void*typedef,和C++是約轉換比C(或目標C)嚴格。你需要在這裏明確地施展:

CFDataRef nativeProp = (CFDataRef)CFReadStreamCopyProperty(...); 
+0

工程太棒了!謝謝! –

相關問題