C++。错误:void 不是指向对象的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7949761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++. Error: void is not a pointer-to-object type
提问by Matt Munson
I have a C++ program:
我有一个 C++ 程序:
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";
}
};
On compile it throws an error:
编译时抛出错误:
error: ‘void*' is not a pointer-to-object type
Can someone explain what I am doing wrong to produce this error?
有人可以解释我做错了什么来产生这个错误吗?
回答by bdonlan
You are dereferencing the void *
before casting it to a concrete type. You need to do it the other way around:
您void *
在将其转换为具体类型之前取消引用。你需要反过来做:
arguments vars = *(arguments *) (args);
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.
这个顺序很重要,因为编译器不知道如何应用*
到args
(它是 avoid *
并且不能被取消引用)。您(arguments *)
告诉它该做什么,但为时已晚,因为取消引用已经发生。
回答by Eric Leschinski
Bare bones example to reproduce the above error:
重现上述错误的裸骨示例:
#include <iostream>
using namespace std;
int main() {
int myint = 9; //good
void *pointer_to_void; //good
pointer_to_void = &myint; //good
cout << *pointer_to_void; //error: 'void*' is not a pointer-to-object type
}
The above code is wrong because it is trying to dereference a pointer to a void. That's not allowed.
上面的代码是错误的,因为它试图取消对指向 void 的指针的引用。那是不允许的。
Now run the next code below, If you understand why the following code runs and the above code does not, you will be better equipped to understand what is going on under the hood.
现在运行下面的下一个代码,如果你理解为什么下面的代码运行而上面的代码不运行,你将更好地理解幕后发生的事情。
#include <iostream>
using namespace std;
int main() {
int myint = 9;
void *pointer_to_void;
int *pointer_to_int;
pointer_to_void = &myint;
pointer_to_int = (int *) pointer_to_void;
cout << *pointer_to_int; //prints '9'
return 0;
}
回答by Mysticial
You have the *
in the wrong place. So you're trying dereference the void*
.
Try this instead:
你*
在错误的地方。所以你正在尝试取消引用void*
. 试试这个:
arguments vars = *(arguments *) (args);
std::cout << "\n" << vars.a << "\t" << vars.b << "\t" << vars.c << "\n";
Alternatively, you can do this: (which also avoids the copy-constructor - as mentioned in the comments)
或者,您可以这样做:(这也避免了复制构造函数 - 如评论中所述)
arguments *vars = (arguments *) (args);
std::cout << "\n" << vars->a << "\t" << vars->b << "\t" << vars->c << "\n";
回答by gandalf
The problem as bdonlan said is "dereferencing void*
before casting".
bdonlan 所说的问题是“void*
在转换之前取消引用”。
I think this example would help:
我认为这个例子会有所帮助:
#include <iostream>
using namespace std;
int main()
{
void *sad;
int s = 23;
float d = 5.8;
sad = &s;
cout << *(int*) sad;//outputs 23//wrong: cout << *sad ;//wrong: cout << (int*) *sad;
sad = &d;
cout << *(float *) sad;//outputs 5.8//wrong: cout << *sad ;//wrong: cout << (float*) *sad;
return 0;
}
回答by Priyanshu Tiwari
The problem above there is that you are trying to deference a void pointer which is not allowed in C or C++.
上面的问题是您试图遵循 C 或 C++ 中不允许的 void 指针。
However, this still works:
但是,这仍然有效:
#include <iostream>
using namespace std;
int main()
{
int b=10;
void *a=&b;
int *ptr=(int*)a;
cout<<*ptr;;
}
We can deference int* pointers after casting void pointers to int* pointers.
在将 void 指针转换为 int* 指针之后,我们可以尊重 int* 指针。
回答by user966379
*args means "the object(value) args points to". Therefore, it can not be casted as pointer to object(argument). That's why it is giving error
*args 表示“对象(值)args 指向”。因此,它不能被转换为指向对象(参数)的指针。这就是为什么它给出错误