C++. Error: void is not a pointer-to-object type(C++.错误:void 不是指向对象的类型)
问题描述
我有一个 C++ 程序:
I have a C++ program:
struct arguments
{
int a, b, c;
arguments(): a(3), b(6), c(9) {}
};
class test_class{
public:
void *member_func(void *args){
arguments vars = (arguments *) (*args); //error: void is not a
//pointer-to-object type
std::cout << "
" << vars.a << " " << vars.b << " " << vars.c << "
";
}
};
编译时报错:
error: ‘void*’ is not a pointer-to-object type
有人可以解释我做错了什么导致这个错误吗?
Can someone explain what I am doing wrong to produce this error?
推荐答案
您在将 void * 转换为具体类型之前取消引用它.你需要反过来做:
You are dereferencing the void * before casting it to a concrete type. You need to do it the other way around:
arguments vars = *(arguments *) (args);
这个顺序很重要,因为编译器不知道如何将 * 应用到 args(这是一个 void * 并且可以' 不会被取消引用).您的 (arguments *) 告诉它该做什么,但为时已晚,因为取消引用已经发生.
This order is important, because the compiler doesn't know how to apply * to args (which is a void * and can't be dereferenced). Your (arguments *) tells it what to do, but it's too late, because the dereference has already occurred.
这篇关于C++.错误:void 不是指向对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++.错误:void 不是指向对象的类型
基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
