【发布时间】:2015-04-09 02:32:57
【问题描述】:
我有这两个结构,它们的头文件附带。
我的结构编号 1 是:
头文件“list.h”:
typedef struct list * List;
源文件“list.c”:
struct list {
unsigned length;
char * value;
};
我的结构号 2 是:
头文件“bal.h”:
typedef enum {START, END, MIDDLE, COMMENTS, CONDITIONS} TypeListBal;
typedef struct bal * Bal;
源文件“bal.c”:
i've include those header files :
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "list.h"
struct bal {
TypeListBal type;
List name; // Name of the bal
List attributes[]; // Array of type struct List
};
当我尝试在我的 bal.c 文件中使用一个函数时:
Bal createBal(List name){
char * search;
const char toFind = '=';
search = strchr(name->value, toFind);
}
我在这一行遇到错误:search = strchr(name->value, toFind);
说:错误:取消引用指向不完整类型的指针
我不知道为什么,因为我已经在我的 bal.c 中包含了 list.h
我读过很多关于 Stackoverflow 的文章,说它被称为这种类型的编程:不透明类型 这似乎真的很好,但我不知道如何将我的其他头文件使用到其他源文件中。 我认为我所要做的就是将我的 list.h 包含在我的 bal.c 文件中。
我在 gcc 中使用这个命令进行编译:gcc -g -W -Wall -c bal.c bal.h
非常感谢!
【问题讨论】:
-
头文件只是在许多其他源文件中包含通用预定义代码的一种便捷方式...一个问题——如果它不在头文件中,它不会被包含在任何源文件中源文件。您的结构定义不在头文件中...
-
不要在编译命令中包含 .h 文件。
-
你至少应该看看Am I correct to assume one cannot forward declare a library's opaque pointer type? 关于同一主题还有其他问题。基本的一点是,如果你有一个不透明的类型,例如
list.h中的List,你不能在指定了确切结构定义的文件之外访问该类型的成员(这意味着只有list.c可以访问@ 987654332@ 或lengthList的成员)。 -
建议:1) 不要 typedef 结构定义。 2)将结构定义放在头文件中。 3) bal.c 需要包含 bal.h 头文件。 4) 将结构声明(实际的结构对象)放在使用它们的文件中。
标签: c gcc compiler-errors opaque-pointers