0

我检查并没有找到任何可以帮助我的材料,所以我不得不问。这是代码:

列表.h:

typedef struct List_t *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->”时,它甚至建议使用 List 的字段自动完成(在这种情况下,我选择了“list2->head”。是什么导致了问题以及如何解决它?谢谢!

4

2 回答 2

2

将 struct List_t 的定义移动到 .h 文件中。

list_example_test.c 没有 struct List_t 的定义,它只知道它是一个 struct(来自 .h 文件),因此编译器无法计算到 List_t 的“head”成员的偏移量。

于 2011-04-26T10:18:18.167 回答
1

List_t就目前而言,是一个不完整的类型list_example_test.c。这实际上是 C 中用于封装数据的常用习惯用法。应该在某处定义函数以允许您在不直接访问列表内部的情况下操作 List_t 类型的项目。你可能会发现你在某处有类似listNext(List_t)或定义的东西。查看与 where声明listIterate(List_t)的文件相同的文件。listCopy()

于 2011-04-26T10:42:39.093 回答