【问题标题】:Unknown type name in C header fileC 头文件中的未知类型名称
【发布时间】:2021-07-21 18:13:30
【问题描述】:

我在编译我的项目时遇到了一个问题。我正在尝试将一个结构放入队列中,该结构已在这样的头文件中贴花:

约会函数头文件(“signupFunctions.h”是我的“患者”声明):

#ifndef appointmentFunctions_H__
#define appointmentFunctions_H__

#include "queueFunctions.h"
#include "signupFunctions.h"

typedef struct{
    int cough;
    int rDistress;
    int fatigue;
    int fever;
}symptoms;

typedef symptoms *Symptoms;

typedef struct {
    Patient patient;
    Symptoms symptoms;
    int day;
    int mon;
    int year;
    int flag;
}appointment;

typedef appointment *Appointment;

int getSymptomsValue(Appointment A);
void setSymptoms(Appointment A);
void requestAppointment(Patient P, qNodePtr *head, qNodePtr *tail);

#endif

队列函数头文件

#ifndef queueFunctions_H__
#define queueFunctions_H__

#include "appointmentFunctions.h"

typedef struct qNode{
    Appointment A;
    struct qNode *next;
}QueueNode;

typedef QueueNode *qNodePtr;

void printQueue(qNodePtr currentPtr);
int isEmpty(qNodePtr head);
void dequeue(qNodePtr *head, qNodePtr *tail);
void enqueue(qNodePtr *head, qNodePtr *tail, Appointment App);

#endif

编译后我得到:

In file included from appointmentFunctions.h:4:0,
                 from menuFunctions.h:5,
                 from main.c:4:
queueFunctions.h:7:2: error: unknown type name 'Appointment'
  Appointment A;
  ^~~~~~~~~~~
queueFunctions.h:16:46: error: unknown type name 'Appointment'
 void enqueue(qNodePtr *head, qNodePtr *tail, Appointment App);
                                              ^~~~~~~~~~~
In file included from appointmentFunctions.h:4:0,
                 from menuFunctions.h:5,
                 from signupFunctions.c:5:
queueFunctions.h:7:2: error: unknown type name 'Appointment'
  Appointment A;
  ^~~~~~~~~~~
queueFunctions.h:16:46: error: unknown type name 'Appointment'
 void enqueue(qNodePtr *head, qNodePtr *tail, Appointment App);
                                          ^~~~~~~~~~~

..等等。有什么问题?提前致谢。

【问题讨论】:

    标签: c struct header include declaration


    【解决方案1】:

    在 AppointmentFunctions.h 中包含 queueFunctions.h

    所以当 queueFunctions.h 被读取时,类型 Appointment 是未知的。

    queueFunctions.h 还包含 AppointmentFunctions.h 对您没有帮助,因为当时 AppointmentFunctions_H__ 已经定义,即不会读取文件内容。

    如果文件 X.h 依赖于文件 Y.h 中的某些内容,同时 Y.h 依赖于文件 X.h 中的某些内容,则您有一个必须修复的设计错误。

    据我所知,依赖只是指向结构的指针,因此您可以通过前向声明结构来摆脱依赖。

    在 queueFunctions.h 中进行以下更改:

    #include "appointmentFunctions.h" --> struct appointment;
    
    Appointment --> struct appointment*
    

    顺便说一句:这是基于意见的,但大多数程序员避免使用 typedef 来表示指针。如果它完成了,通常最好使用一个清楚地表明这是一个指针的名称,例如:

    typedef appointment *Appointment; --> typedef appointment *appointmentPtr;
    

    【讨论】:

      【解决方案2】:

      您递归地将一个标题包含在另一个标题中

      #ifndef appointmentFunctions_H__
      #define appointmentFunctions_H__
      
      #include "queueFunctions.h"
      #include "signupFunctions.h"
      //...
      

      #ifndef queueFunctions_H__
      #define queueFunctions_H__
      
      #include "appointmentFunctions.h"
      //...
      

      所以编译器会报错。

      你不能那样做。

      即标头appointmentFunctions.h立即包含标头queueFunctions.h,其中有声明

      typedef struct qNode{
          Appointment A;
          struct qNode *next;
      }QueueNode;
      

      Appointment 的声明尚不可见。在包含标题 queueFunctions.h 之后。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-11
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        • 2013-09-25
        • 2018-10-24
        • 2012-02-06
        • 1970-01-01
        相关资源
        最近更新 更多