开发者

error: dereferencing pointer to incomplete type

开发者 https://www.devze.com 2023-01-13 17:39 出处:网络
i have the following code parts: typedef struct Object* ObjectP; ObjectP CreateObject(void *key) { printf(\"GOT %d\\n\",(*(int*) key));

i have the following code parts:

typedef struct Object* ObjectP;

ObjectP CreateObject(void *key) {

    printf("GOT %d\n",(*(int*) key));
    ObjectP objP = (ObjectP开发者_运维问答) malloc(sizeof(Object));
    if (objP == NULL) {
        //TODO ReportError(MEM_OUT);
        return NULL;
    }
    objP->_key = key;
    objP->_next = NULL;
    objP->_numInChain = 1;

    return objP;

}

typedef struct Object {
  ObjectP _next;
  void* _key;
  int _numInChain;
} Object;

and in another file:

void IntPrint(const void *key) {
    assert(key != NULL);
    printf("%d",*((const int*)key));
}

and in another file I have the main() :

int main(int argc, char* argv[]) {
 int* key = (int*) malloc(sizeof(int));
 *key = 20;
 ObjectP a = CreateObject(key);
 IntPrint(a->_key);  //THIS DOESN'T COMPILE
 return 0;
}

IntPrint doesn't compile. it writes:

error: dereferencing pointer to incomplete type

and i can't understand why, because IntPrint recieves void* and a->_key is also void*.

Thanks!


You posted a bunch of virtually irrelevant pieces of code, but omitted the most important one: what is ObjectP and how it is defined? The compiler tells you that it is defined as a pointer to incomplete type. That's your problem. What exactly is wrong with the definition is impossible to say without actually seeing it.

After the edit: Your problem is that the definition of struct Object is not visible at the point where you are trying to access a->key (in.e. in main). You either forgot to include it or something like that. Is the definition of struct Object located in the same header file as definition of ObjectP?


Where do you have ObjectP defined? I suggest not declaring it before Object is fully defined:

typedef struct Object {
  struct Object*  _next;
  void* _key;
  int _numInChain;
} Object;

typedef Object* ObjectP;

This way it should be always safe to use ObjectP. However, without you posting more code, this is just a blind guess.


Try the following in your main function:

IntPrint((const void *) a->_key);

The definition of _key in the structure Object doesn't have the modifier constant and that could cause a warning or an error depending the flags you're using when compiling.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号