繁体   English   中英

C-未知类型名称

[英]C - Unknown type name

我需要为大学建立一个“社交网络”,但是在编译时我总是得到未知的类型名称“ List”。 我从标头中删除了很多功能,但是仍然出现相同的错误,我也不知道为什么。 我有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);
}

谢谢阅读。

这是我尝试编译这组文件时遇到的(第一个)错误:

$ cc main.c
In file included from main.c:4:
In file included from ./ListHeadTail.h:4:
In file included from ./Student.h:6:
./Friend.h:11:19: error: unknown type name 'List'
void printFriends(List *l);

查看文件名和行号。 请注意,在ListHeadTail.h第4行中,您已经定义了LISTHEADTAIL_H ,但尚未到达List的实际声明。 然后,您进入Student.h,并从那里进入Friend.h。 再次包含ListHeadTail.h,但是由于已经定义了LISTHEADTAIL_H ,因此该include不会执行任何操作。 因此,您将继续通过Friend.h而不使用List声明,因此在引用它的声明中会出现错误。

正如@lurker在评论中指出的那样,这里的基本问题是循环依赖,而简单的解决方法是前向声明。 在这种情况下,您可以简单地修改Friend.H,用typedef struct list List;替换#include "ListHeadTail.h" typedef struct list List;

但是对我来说,这有点hacky。 如果将include的顺序更改到某个位置,则构建可能会再次中断。

我认为真正的问题是函数的声明( printFriends等)不属于Friend.h; 它们属于ListHeadTail.h。 这些功能与Friend类型无关。 当然,它们的名称中带有“ Friend”,但是声明中引用的唯一类型是List 因此,它们属于ListHeadTail.h。 Student.h中的changeData函数也是如此。

在面向对象的设计中(例如,在Java中),这些函数都可能是List类的方法,并将在该类的源文件中声明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM