本文介绍了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 不是指向对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)