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