【问题标题】:another "dereferencing point to incomplete type" question另一个“解除对不完整类型的引用”问题
【发布时间】:2011-04-26 10:13:59
【问题描述】:

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

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

【问题讨论】:

    标签: c struct dereference


    【解决方案1】:

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

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

    【讨论】:

    • 我知道它会这样工作,但问题是我不能更改 .h 文件,因为这是一个家庭作业(我们不允许更改它)。一定有什么办法……
    • @Yanal:如果你不能更改标题,那么你只能使用那里声明的方法来操作List:这是一个不透明的数据结构,你(即你的主程序)不知道它看起来如何。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-18
      • 2012-10-13
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      相关资源
      最近更新 更多