C++. Error: void is not a pointer-to-object type

元气小坏坏 提交于 2019-11-26 23:13:58

问题


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";
    }
};

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?


回答1:


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);

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.




回答2:


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.

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;
}



回答3:


You have the * in the wrong place. So you're trying dereference the void*. Try this instead:

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";



回答4:


The problem as bdonlan said is "dereferencing void* before casting".

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;
}



回答5:


*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




回答6:


The problem above there is that you are trying to deference a void pointer which is not allowed in C or C++.

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.



来源:https://stackoverflow.com/questions/7949761/c-error-void-is-not-a-pointer-to-object-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!