简体   繁体   English

从可可项目中的不兼容类型“ void *”错误分配给“ MyPrivateData *”

[英]Assigning to “MyPrivateData *” from incompatible type 'void *' error in cocoa project

I'm trying to implement the USBPrivateDataSample in my cocoa project. 我正在尝试在可可项目中实现USBPrivateDataSample The problem is when I'm trying to allocate memory for the struct MyPrivateData I get the error 问题是,当我尝试为MyPrivateData结构分配内存时,出现错误

"Assining to "MyPrivateData *" from incombatible type 'void *'" “从不可竞争的类型'void *'关联到“ MyPrivateData *””

I have the struct definition in my header file: 我的头文件中有结构定义:

#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{

... ...

And I'm calling in my .mm file: 我正在调用我的.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));

Any useful advice? 有什么有用的建议吗?

The suffix mm means you are using C++ code and Objective-C code. 后缀mm表示您正在使用C ++代码和Objective-C代码。 Although Objective-C is a superset op C, the compiler will allow it. 尽管Objective-C是op C的超集,但编译器将允许它。 But you have to keep in mind that C++ is not a superset of C. The same rules don't apply. 但是您必须记住,C ++不是C的超集。相同的规则不适用。

While C allows you to do implicit casts from void * to an other data type, C++ requires you to do an explicit cast. 尽管C允许您从void *隐式转换为其他数据类型,但是C ++要求您进行显式转换。

Eg: 例如:

char *a;
void *b;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:从不兼容类型void分配 - Error: Assigning from incompatible type void 从不兼容类型&#39;int(^ __ strong)(void)&#39;分配给&#39;int&#39; - Assigning to 'int' from incompatible type 'int (^__strong)(void)' 从不兼容的类型 &#39;void (^__strong)(int, const char *, int)&#39; 分配给 &#39;void (*)(int, const char *, int)&#39; - Assigning to 'void (*)(int, const char *, int)' from incompatible type 'void (^__strong)(int, const char *, int)' 从不兼容的类型&#39;void *&#39;分配&#39;uint8_t *&#39;(又名&#39;unsigned char *&#39;) - assigning to 'uint8_t *' (aka 'unsigned char *' ) from incompatible type 'void *' 构建错误:分配给&#39;id <UIPickerViewDataSource> &#39;来自不兼容的类型&#39;NSArray *&#39; - Build error: Assigning to 'id<UIPickerViewDataSource>' from incompatible type 'NSArray *' 如何解决此错误(分配给&#39;id <InputStream> &#39;来自不兼容类型&#39;NSStream *&#39;)? - how to solve this error (Assigning to 'id<InputStream>' from incompatible type 'NSStream *')? 从不兼容类型“ id”分配给“ float” - Assigning to 'float' from incompatible type 'id' 分配浮点值:“不兼容类型”错误 - Assigning float values: “incompatible type” error 分配给&#39;id <NSTextViewDelegate> &#39;来自不兼容的类型 - Assigning to 'id<NSTextViewDelegate>' from incompatible type 从不兼容类型'id <UIApplicationDelegate>'分配给'AppDelegate *' - Assigning to 'AppDelegate *' from incompatible type 'id<UIApplicationDelegate>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM