tags:

views:

71

answers:

3

Hi, 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!

+3  A: 

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?

AndreyT
sorry, i'll edit in right away
Mike
+2  A: 

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.

dark_charlie
i don't think it's the problem. typedef ObjectP is defined in GenericHashMap.h (in the header), and the struct Object is defined in GenericHashMap.c.. i'm including the header at the beginning.
Mike
@Asher: Well, that's your problem. You defined `struct Object` in a .c file, so it is not visible to your `main` function. So you get an incomplete type error, as you should. (I assume `main` function is in a different .c file).
AndreyT
A: 

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.

msalvadores