gpt4 book ai didi

objective-c - 从 cocoa 项目中的不兼容类型 "MyPrivateData *"错误分配给 'void *'

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:43 24 4
gpt4 key购买 nike

我正在尝试实现 USBPrivateDataSample在我的 cocoa 项目中。问题是当我尝试为 MyPrivateData 结构分配内存时出现错误

"Assining to "MyPrivateData *" from incombatible type 'void *'"

我的头文件中有结构定义:

#define kMyVendorID     0x0403
#define kMyProductID 0x6001

class RtMidiOut;

typedef struct MyPrivateData {
io_object_t notification;
IOUSBDeviceInterface **deviceInterface;
CFStringRef deviceName;
UInt32 locationID;
} MyPrivateData;

static IONotificationPortRef gNotifyPort;
static io_iterator_t gAddedIter;
static CFRunLoopRef gRunLoop;


@interface serialInput : NSObject{

...

我正在调用我的 .mm 文件:

void DeviceAdded(void *refCon, io_iterator_t iterator){
kern_return_t kr;
io_service_t usbDevice;
IOCFPlugInInterface **plugInInterface = NULL;
SInt32 score;
HRESULT res;

while ((usbDevice = IOIteratorNext(iterator))) {
io_name_t deviceName;
CFStringRef deviceNameAsCFString;
MyPrivateData *privateDataRef;
UInt32 locationID;

printf("Device added.\n");

// Add some app-specific information about this device.
// Create a buffer to hold the data.

privateDataRef = malloc(sizeof(MyPrivateData)); //The error!

bzero(privateDataRef, sizeof(MyPrivateData));

有什么有用的建议吗?

最佳答案

后缀mm 表示您正在使用C++ 代码和Objective-C 代码。尽管 Objective-C 是 op C 的超集,但编译器会允许它。但是你必须记住 C++ 不是 C 的超集。同样的规则不适用。

虽然 C 允许您从void * 到其他数据类型,C++ 要求您进行显式转换。

例如:

char *a;
void *b;

a = b; // allowed in C, not in C++
a = (char *)b; // allowed in C, required in C++

关于objective-c - 从 cocoa 项目中的不兼容类型 "MyPrivateData *"错误分配给 'void *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10861146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com