【发布时间】:2017-11-04 11:55:06
【问题描述】:
我将一个 C 程序拆分为多个文件。这是它们的样子。
ListDefinition.h:
#ifndef ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
#define ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
extern typedef struct type DataType; //this is a struct declaration about basic data type in the list
extern typedef struct s_list SeqList, *PseqList; //this is a struct declaration about sequence list
extern typedef struct sl_list SlinkList, *PSlinkList; //this is a struct declaration about linked list
#endif //ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
或者我删除extern
#ifndef ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
#define ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
typedef struct type DataType; //this is a struct declaration about basic data type in the list
typedef struct s_list SeqList, *PseqList; //this is a struct declaration about sequence list
typedef struct sl_list SlinkList, *PSlinkList; //this is a struct declaration about linked list
#endif //ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
ListDefinition.c:
#include "ListDefinition.h"
#define MAXSIZE 100
typedef struct type {
int date;
}DataType;
typedef struct s_list {
DataType a[MAXSIZE];
int length;
int top;
}SeqList, *PseqList;
typedef struct sl_list {
DataType node;
struct sl_list *next;
}SlinkList, *PSlinkList;
我想在ListFunction.h 中使用ListDefinition.h
ListFunction.h:
#ifndef ALGORITHM_AND_DATASTRUCTURE_LISTFUNCTION_H
#define ALGORITHM_AND_DATASTRUCTURE_LISTFUNCTION_H
#include "ListDefinition.h"
PseqList initial_seqList(PseqList PL);//The function be used to initialize the sequence list
int search_seqlist(PseqList PL, DataType x);//the function be used to search the x in the sequence list
#endif //ALGORITHM_AND_DATASTRUCTURE_LISTFUNCTION_H
ListFunction.c:
#include <stdio.h>
#include <stdlib.h>
#include "ListFunction.h"
PseqList initial_seqList(PseqList PL) {
PL=malloc(sizeof(SeqList));
if(PL == NULL) {
exit(1);
printf("The memory isn't allocated");
}
PL->length = 0;
PL->top = -1;
return PL;
}
int search_seqlist(PseqList PL, DataType x) {
int i;
for(i = 0;i < PL->length; i++) {
if(PL->a[i].date == x.date)
break;
}
if (i == PL->length)
return 0;
else
return i+1;
}
您不关心代码的含义。出现了很多错误,但是当我在ListFunction.h 中将#include "ListDefinition.h" 更改为#include "ListDefinition.c" 时。所有错误都消失了,我想知道为什么? This problem 似乎告诉我应该使用 ListFunction.h。我在 Clion 中运行代码。
【问题讨论】:
-
我想问题是
ListDefinition.c包含应该在.h 文件中的声明。我以前从未见过extern typedef struct,也不知道那是什么意思。 -
extern关键字用于在编译后链接数据对象或函数。尽管如此,编译所需的所有信息都必须在文件中可用。因此,您不能有类似extern类型的声明。如果您主动隐藏定义,编译器应该如何知道SeqList的外观。 -
@Gerhardh 如果我删除 extern?
-
请永远不要以这种方式更新您的问题,让所有 cmets 无用!
-
删除
extern没有帮助。类型定义仍然缺失。
标签: c