开发者

another "dereferencing point to incomplete type" question

i checked and didnt find any material which can help me so i had to ask. this is the code:

list.h:

typedef struct List_t *List;

list.c:

#include "list.h"
#include <stdlib.h>
#include <stdio.h>


typedef struct Item_t
{
    struct Item_t* next;
    ListElement data;
}*Item;

typedef struct List_t
{
    Item head;
    Item iterator;
    CopyListElement copyFu开发者_JAVA百科nc;
    FreeListElement freeFunc;
};

list_example_test.c:

#include "list.h"
#include <stdlib.h>
#include <stdio.h>
ListElement copyA(ListElement a)
{
    return a;
}
void destroyA(ListElement a)
{
}
bool testListCreate();
bool testListCopy()
{
    List list1=listCreate(copyA,destroyA);
    listInsertFirst(list1,(void*)6);
    listInsertFirst(list1,(void*)2);
    listInsertFirst(list1,(void*)1);
    List list2=listCopy(list1);
    listClear(list1);
    if(list2->head==NULL) //here is the error!!
    {
        return false;
    }
    return true;
}

the last piece of code is supposed to check whether the listCopy() function works. the compiler recognizes the name List and when i type "list2->" it even suggests to autocomplete with the fields of List(in this instance i chose "list2->head". what is causing the problem and how to fix it? thanks!


Move the definition of struct List_t to the .h file.

The list_example_test.c does not have a definition of struct List_t, it only knows that it's a struct (from the .h file), so the compiler has no way to calculate the offset to the "head" member of the List_t.


List_t is an incomplete type as far as list_example_test.c is concerned. This is actually a common idiom in C for encapsulating data. There should be functions defined somewhere to allow you to manipulate items of type List_t without directly accessing the internals of the list. You'll probably find you have something like listNext(List_t) or listIterate(List_t) defined somewhere. Look in the same file as where listCopy() is declared.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜