【发布时间】:2014-10-02 16:02:11
【问题描述】:
好的,让我开始这个问题。已提出此问题,但未针对未更改 .h 文件的情况专门回答此问题,因此请不要链接到已通过说“哦,将其移至 .h 文件”来回答的问题。我目前正在上课,老师将 .h 文件交给我们并说,对此进行实施并且不要更改 .h 文件中的任何内容。我按照建议将结构等移动到我之前项目的 .h 文件中,因此我的成绩受到了很大的打击
话虽如此,让我发布一些代码并深入了解细节。
.h 文件
#ifndef _list_h
#define _list_h
typedef char *ListItemP //a pointer to an item in the list
typedef struct List *ListP //a pointer to the List struct itself
somefunctions();
#endif
.c 文件
#include "List.h"
struct List
{
int foo;
ListItemP bar;
};
ListP newList()
{
// implementation, I understand and am confident I used malloc
// and everything else correctly to make a new struct
}
main.c 或 tester.c 或任何你想称之为的文件,我在其中测试实现和接口(分别为 .c 和 .h)
#include <stdio.h>
#include <stlib.h>
#include "List.h"
int main()
{
ListP n1 = newList();
n1->foo = 5;
return 0;
}
这些是我在我的 unix (osx) 终端上使用 gcc -O List.c main.c 编译后遇到的错误
main.c:16:5: error: incomplete definition of type 'struct List'
n1->foo = 5;
~~^
./List.h:21:16: note: forward declaration of 'struct List'
typedef struct List *ListP;
^
1 error generated.
我们将不胜感激任何帮助,如果您坚持将我链接到以前提出的问题,请指出他们在该问题中的哪个位置回答了我的问题的特定部分。也许我错过了,但我觉得我已经非常彻底地检查了这些问题。自本课程开始以来,这个问题就一直困扰着我,所以一段代码可以让我直截了当,但是我希望得到一个解释,这样我才能最终理解我做错了什么。谢谢谢谢谢谢。
【问题讨论】:
-
您需要在 main.c 文件中包含列表 .c 文件的内容(即使用单个源文件)。如果头文件中有结构声明,则可以使用不同的列表实现文件。
-
您应该尝试仅使用
list.h中声明的函数和类型。如果您需要测试访问实现细节的实现,请将测试代码添加到list.cc。
标签: c struct forward-declaration incomplete-type