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

前端 未结 6 1243
一生所求
一生所求 2020-12-06 04:35

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 *arg         


        
6条回答
  •  旧时难觅i
    2020-12-06 05:25

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

    I think this example would help:

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

提交回复
热议问题