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 不是指向对象的类型
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
