gpt4 book ai didi

将 void* 转换为其他类型

转载 作者:行者123 更新时间:2023-11-30 18:28:52 24 4
gpt4 key购买 nike

我收到错误:

error: 'void*' is not a pointer-to-object type

对于以下代码:

Button config_b1 = Button("b1");
Button config_b2 = Button("b2");
Button config_b3 = Button("b3");

const void *const buttons[3] PROGMEM = {
&config_b1, &config_b2, &config_b3
};

在某些功能中:

(Button)*buttons[1]->setText("Foo");

转换为 Button 失败。

这只是一个简化的示例。数组“按钮”将具有不同类型的对象,因此我需要将其初始化为 void,然后在函数内转换为正确的类型。

最佳答案

我认为这是一个简单的优先级错误,您需要

((Button *) buttons[1])->setText("foo");

顺便问一下,你确定这是C吗?这种调用确实看起来像 C++(也可能是 C,但在大多数情况下您需要显式的 this 等效项)。

关于将 void* 转换为其他类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44135142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com