2

ココア プロジェクトにUSBPrivateDataSampleを実装しようとしています。問題は、構造体 MyPrivateData にメモリを割り当てようとしているときにエラーが発生することです

「互換性のないタイプ 'void *' から "MyPrivateData *" に代入しています」

ヘッダー ファイルに構造体定義があります。

#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));

役に立つアドバイスはありますか?

4

1 に答える 1

2

サフィックス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++
于 2012-06-03T07:01:19.027 に答える