C ++。错误:void不是指针到对象类型 [英] 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 << "\n" << vars.a << "\t" << vars.b << "\t" << vars.c << "\n";
}
};
在编译时会引发错误:
error: ‘void*’ is not a pointer-to-object type
$ b b
有人可以解释我做错了什么以产生此错误吗?
Can someone explain what I am doing wrong to produce this error?
推荐答案
c> 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不是指针到对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文