简体   繁体   English

C++,“错误:'void*' 不是指向对象的类型”

[英]C++, “error: 'void*' is not a pointer-to-object type”

I need to create an array of pointers of different types.我需要创建一个不同类型的指针数组。 Some variables are defined at the beginning and their vectors are assigned to different spaces in the array.一些变量在开头定义,它们的向量被分配到数组中的不同空间。 Later, from another process, the values of these variables can be changed using the pointers stored in the array.稍后,在另一个进程中,可以使用存储在数组中的指针来更改这些变量的值。 Variables are accessed directly from the main process (without pointers)变量直接从主进程访问(不带指针)

In de main process at start:在 de main 进程中:

void *values[255];
uint8_t val1 = 12;
uint16_t val2 = 3478;

SetIDVariable(&val1, 0);
SetIDVariable(&val2, 2);

Others functions:其他功能:

void SetIDVariable(uint8_t *variable, uint8_t id) {
  values[id] = variable;
}

void SetIDVariable(uint16_t *variable, uint8_t id) {
  values[id] = variable;
}

In other process (x is a any number for check if value change):在其他过程中(x 是任意数字,用于检查值是否发生变化):

values[0] = (void*)(val1 + x);
values[2] = (void*)(val2 + x);

In main process:在主要过程中:

Serial.print("Value 1: "); Serial.println(val1);
Serial.print("Value 2: "); Serial.println(val2);

Values are always 12 and 3478. Any ideas?值总是 12 和 3478。有什么想法吗?

You not change val1 and val2 with this code.您不会使用此代码更改 val1 和 val2。 You just affect new pointer allocation in values您只会影响值中的新指针分配

values[0] = (void*)(val1 + x);
values[2] = (void*)(val2 + x);

You are converting an integer value to pointer.您正在将整数值转换为指针。 Maybe you wanted to do this:也许你想这样做:

*((uint8_t *) values[0]) = (*((uint8_t *) values[0])+x);
*((uint16_t *) values[2]) = (*((uint16_t *) values[2])+x);

also see these questions: this question and this question另请参阅这些问题: 这个问题这个问题

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

相关问题 C ++。 错误:void不是指向对象的指针类型 - C++. Error: void is not a pointer-to-object type 错误错误:“ void *”不是指向对象的指针类型 - Error error: ‘void*’ is not a pointer-to-object type 在C ++中,我收到一条消息“错误:'void *'不是指向对象的指针类型” - In C++, I'm getting a message “error: 'void*' is not a pointer-to-object type” 'void *'不是指向对象的指针类型 - ‘void*’ is not a pointer-to-object type C ++数组指针到对象的错误 - C++ Array pointer-to-object error 错误:“ void *”不是指向对象的指针类型正确的解决方案是什么? - error: ‘void*’ is not a pointer-to-object type what is the right solution? sqrt 函数导致“'void*' 不是指向对象类型的指针”错误 - sqrt function causing " 'void*' is not a pointer-to-object type" error C++ 表达式必须具有指向对象的类型 - C++ Expression must have pointer-to-object type 函数内部的 C++ 错误:表达式必须具有指向对象的类型 - C++ error inside function: expression must have pointer-to-object type 错误:即使指针设置为对象,为什么“void*”也不是指向对象的指针类型? - Error: Why 'void*' is not a pointer-to-object type even though the pointer is set to an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM