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

Matt Munson picture Matt Munson · Oct 31, 2011 · Viewed 46.2k times · Source

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?

Answer

bdonlan picture bdonlan · Oct 31, 2011

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.