linux从类型void*到类型data*的转换无效,编译时出错:从'void*'到'unsigned char*'的无效转换...
时间: 2023-06-17 21:08:40 浏览: 145
这个错误可能是因为您试图将 `void*` 类型的指针转换为 `data*` 类型的指针,但是编译器无法确定如何进行转换,因为 `void*` 类型的指针不指向任何特定类型的数据。要解决这个问题,您需要将 `void*` 类型的指针转换为 `unsigned char*` 类型的指针,然后再将其转换为 `data*` 类型的指针。示例代码如下:
```
void* ptr = malloc(sizeof(data));
data* dp = (data*)((unsigned char*)ptr);
```
在这个示例中,我们首先将 `void*` 类型的指针 `ptr` 转换为 `unsigned char*` 类型的指针,然后再将其转换为 `data*` 类型的指针 `dp`。这样可以避免编译器报错,并且能够正确地将 `void*` 类型的指针转换为 `data*` 类型的指针。
相关问题
pullflowmonitor.cpp: In member function 'bool PullFlowMonitor::initModel()': pullflowmonitor.cpp:1223:99: error: cannot convert 'uint8_t* {aka unsigned char*}' to 'void**' for argument '1' to 'aclError aclrtMallocCached(void**, size_t, aclrtMemMallocPolicy)' AclInputs.inputs[i].data,sizeof(uint8_t)*inputsize,ACL_MEM_MALLOC_HUGE_FIRST);
这个错误是因为aclrtMallocCached函数的第一个参数是一个指向void*类型的指针,而你的代码中传递的是一个指向uint8_t*类型的指针,这样会导致类型不匹配,编译器无法编译通过。
解决这个问题,可以将aclrtMallocCached函数的第一个参数的类型改为void*类型的指针。
例如:
```c++
void* buffer;
aclError ret = aclrtMallocCached(&buffer, size, ACL_MEM_MALLOC_HUGE_FIRST);
```
或者使用reinterpret_cast进行类型转换:
```c++
uint8_t* buffer;
aclError ret = aclrtMallocCached(reinterpret_cast<void**>(&buffer), size, ACL_MEM_MALLOC_HUGE_FIRST);
```
这样就可以避免这个编译错误了。
C:\Users\conservator\Desktop\mqttToAliyun\mqttToAliyun.ino: In function 'void get_value()': C:\Users\conservator\Desktop\mqttToAliyun\mqttToAliyun.ino:128:34: error: no matching function for call to 'println(const char [3], int&)' 128 | Serial.println("%d",sensor[0]); | ^ In file included from C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Stream.h:27, from C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/HardwareSerial.h:32, from C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Arduino.h:303, from C:\Users\conservator\AppData\Local\Temp\arduino\sketches\304EB848CF381B8A7A8E84D646752A81\sketch\mqttToAliyun.ino.cpp:1: C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Print.h:102:16: note: candidate: 'size_t Print::println(unsigned char, int)' (near match) 102 | size_t println(unsigned char, int = DEC); | ^~~~~~~ C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Print.h:102:16: note: conversion of argument 1 would be ill-formed: C:\Users\conservator\Desktop\mqttToAliyun\mqttToAliyun.ino:128:20: error: invalid conversion from 'const char*' to 'unsigned char' [-fpermissive] 128 | Serial.println("%d",sensor[0]); | ^~~~ | | | const char* In file included from C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Stream.h:27, from C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/HardwareSerial.h:32, from C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Arduino.h:303, from C:\Users\conservator\AppData\Local\Temp\arduino\sketches\304EB848CF381B8A7A8E84D646752A81\sketch\mqttToAliyun.ino.cpp:1: C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Print.h:103:16: note: candidate: 'size_t Print::println(int, int)' (near match) 103 | size_t println(int, int = DEC); | ^~~~~~~ C:\Users\conservator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Print.h:103:16: note: conversion of argument 1 would be ill-formed: C:\Users\conservator\Desktop\mqttToAliyun\mqttToAliyun.ino:128:20: error: invalid conversion from 'const char*' to 'int' [-fpermissive] 128 | Serial.println("%d",sensor[0]);
这个错误是因为在你的代码中,`Serial.println()` 函数的参数传递方式不正确。`println()` 函数的第一个参数应该是一个字符串,而你传递了一个格式化字符串。正确的用法是使用 `Serial.print()` 函数来打印格式化字符串。以下是修正后的代码示例:
```cpp
Serial.print("%d");
Serial.println(sensor[0]);
```
这样就能正确打印传感器值了。记得在修改完代码后重新编译和上传到你的硬件设备上。
阅读全文