简体   繁体   中英

C - Unknown type name

I need to build a "social network" for college, but I always get unknown type name 'List' while compiling. 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:

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 . You then go into Student.h, and from there into Friend.h. That includes ListHeadTail.h again -- but since LISTHEADTAIL_H is already defined, this include does nothing. So you continue through Friend.h with no declaration of List , and therefore get an error on the declarations that reference it.

As noted by @lurker in their comment, the basic issue here is circular dependency, and a simple fix is forward declaration. In this case, you could simply modify Friend.H, replacing #include "ListHeadTail.h" with typedef struct list List; .

But to me this is a bit hacky. If you shift the order of includes somewhere, the build might break again.

I think the real problem is that the declarations of the functions ( printFriends , etc.) don't belong in Friend.h; they belong in ListHeadTail.h. The functions have nothing to do with the Friend type. Sure, they have "Friend" in their names, but the only type referenced in the declarations is List . So they belong in ListHeadTail.h. Same goes for the changeData function in Student.h.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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