[英]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.