开发者

Invalid conversion from const void* error

开发者 https://www.devze.com 2023-03-22 07:03 出处:网络
I am running the following code in an .mm file and I get the error: Invalid conversion from \'const void*\' to \'const __CFData*\'

I am running the following code in an .mm file and I get the error:

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

I need to run the code in .mm. If I change to .m it doesn't complain. Why is it behaving like this? I compile to 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;
}


CFReadStreamCopyProperty() returns CFTypeRef, which is just a typedef for const void*, and C++ is stricter about conversions than C (or Objective-C). You need to cast explicitly here:

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

精彩评论

暂无评论...
验证码 换一张
取 消