简体   繁体   English

C-未知类型名称

[英]C - Unknown type name

I need to build a "social network" for college, but I always get unknown type name 'List' while compiling. 我需要为大学建立一个“社交网络”,但是在编译时我总是得到未知的类型名称“ List”。 I removed a lot of functions from my headers, but I still get the same error and I don't know why. 我从标头中删除了很多功能,但是仍然出现相同的错误,我也不知道为什么。 I've got 3 headers: 我有3个标题:

My friend's header 我朋友的标题

#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 */

My list header: 我的清单标题:

#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 */

My student's header 我学生的标题

#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 */

My main: 我的主要:

#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);
}

Thanks for reading. 谢谢阅读。

Here's the (first) error I get when I try to compile this set of files: 这是我尝试编译这组文件时遇到的(第一个)错误:

$ 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);

Look at the file names and line numbers. 查看文件名和行号。 Note that at ListHeadTail.h line 4, you've already defined LISTHEADTAIL_H , but you haven't yet reached the actual declaration of List . 请注意,在ListHeadTail.h第4行中,您已经定义了LISTHEADTAIL_H ,但尚未到达List的实际声明。 You then go into Student.h, and from there into Friend.h. 然后,您进入Student.h,并从那里进入Friend.h。 That includes ListHeadTail.h again -- but since LISTHEADTAIL_H is already defined, this include does nothing. 再次包含ListHeadTail.h,但是由于已经定义了LISTHEADTAIL_H ,因此该include不会执行任何操作。 So you continue through Friend.h with no declaration of List , and therefore get an error on the declarations that reference it. 因此,您将继续通过Friend.h而不使用List声明,因此在引用它的声明中会出现错误。

As noted by @lurker in their comment, the basic issue here is circular dependency, and a simple fix is forward declaration. 正如@lurker在评论中指出的那样,这里的基本问题是循环依赖,而简单的解决方法是前向声明。 In this case, you could simply modify Friend.H, replacing #include "ListHeadTail.h" with typedef struct list List; 在这种情况下,您可以简单地修改Friend.H,用typedef struct list List;替换#include "ListHeadTail.h" typedef struct list List; .

But to me this is a bit hacky. 但是对我来说,这有点hacky。 If you shift the order of includes somewhere, the build might break again. 如果将include的顺序更改到某个位置,则构建可能会再次中断。

I think the real problem is that the declarations of the functions ( printFriends , etc.) don't belong in Friend.h; 我认为真正的问题是函数的声明( printFriends等)不属于Friend.h; they belong in ListHeadTail.h. 它们属于ListHeadTail.h。 The functions have nothing to do with the Friend type. 这些功能与Friend类型无关。 Sure, they have "Friend" in their names, but the only type referenced in the declarations is List . 当然,它们的名称中带有“ Friend”,但是声明中引用的唯一类型是List So they belong in ListHeadTail.h. 因此,它们属于ListHeadTail.h。 Same goes for the changeData function in Student.h. Student.h中的changeData函数也是如此。

In an object-oriented design (say, in Java), these functions would all probably be methods of the List class, and would be declared in that class's source file. 在面向对象的设计中(例如,在Java中),这些函数都可能是List类的方法,并将在该类的源文件中声明。

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

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