我检查过并没有发现任何可以帮助我的材料,所以我不得不问。这是代码:
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 copyFunc;
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;
}
最后一段代码应该检查listCopy()函数是否有效。编译器识别名称List,当我输入“list2-&gt;”时它甚至建议用List的字段自动完成(在这个例子中我选择了“list2-&gt; head”。 是什么导致了问题以及如何解决?谢谢!
答案 0 :(得分:2)
将struct List_t的定义移动到.h文件。
list_example_test.c没有struct List_t的定义,它只知道它是一个struct(来自.h文件),因此编译器无法计算到List_t的“head”成员的偏移量
答案 1 :(得分:1)
List_t
而言, list_example_test.c
是一种不完整的类型。这实际上是C中用于封装数据的常用习惯用法。应该在某处定义函数,以允许您操作List_t类型的项而不直接访问列表的内部。您可能会发现某处定义了listNext(List_t)
或listIterate(List_t)
之类的内容。查看与声明listCopy()
的文件相同的文件。