【发布时间】:2018-10-27 03:45:09
【问题描述】:
我需要为大学建立一个“社交网络”,但我在编译时总是得到未知的类型名称“列表”。我从标题中删除了很多函数,但我仍然得到同样的错误,我不知道为什么。 我有 3 个标题:
我朋友的标题
#ifndef FRIEND_H
#define FRIEND_H
#include "ListHeadTail.h"
typedef struct Friend{
int id;
struct Friend *nextFriend;
}Friend;
void printFriends(List *l);
void removeFriend(List *l);
void addFriend(List *l);
#endif /* FRIEND_H */
我的列表标题:
#ifndef LISTHEADTAIL_H
#define LISTHEADTAIL_H
#include "Student.h"
typedef struct pStudent{
struct pStudent *ant;
Student *s;
struct pStudent *prox;
}pStudent;
typedef struct list{
pStudent *head;
pStudent *tail;
}List;
void startList(List *l);
void printList(List *l);
void freeList(List *l);
#endif /* LISTHEADTAIL_H */
我学生的标题
#ifndef STUDENT_H
#define STUDENT_H
#define MAX 51
#include "Friend.h"
#include "ListHeadTail.h"
typedef struct Student{
int id;
char name[MAX];
Friend *friends;
}Student;
Student* readStudent ();
void printStudent(Student* a);
void changeData(List *l);
#endif /* STUDENT_H */
我的主要:
#include <stdio.h>
#include <stdlib.h>
#include "ListHeadTail.h"
#include "Friend.h"
#include "Student.h"
int main(int argc, char** argv) {
List l;
startList(&l);
freeList(&l);
return (EXIT_SUCCESS);
}
感谢阅读。
【问题讨论】:
-
您的
main首先包括ListHeadTail.h,其中将包括Student.h。然后Student.h将包括Friend.h,全部按此顺序。Student.h将在实际定义之前尝试使用ListHeadTail.h中定义的内容(ListHeadTail.h的完整主体尚未被解析)。当你有循环依赖时,你需要使用forward declarations。 -
你是对的。我刚刚更改了我的代码,现在它可以工作了。谢谢。