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

前端 未结 6 1242
一生所求
一生所求 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条回答
  •  无人及你
    2020-12-06 05:15

    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 
    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.

提交回复
热议问题