const void *错误导致转换无效

时间:2011-07-20 23:58:26

标签: iphone c++ objective-c

我在.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;
}

1 个答案:

答案 0 :(得分:5)

CFReadStreamCopyProperty()返回CFTypeReftypedef只是const void*,而C ++对转化的反应比C(或Objective-C)更严格。你需要在这里明确地投射:

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