c - 错误 : unknown type in C. 包含所有必需的头文件时可能是什么原因?

标签 c

所以我无法弄清楚以下问题: 我得到 orderPtr 的未知类型,它是 struct Order 的 typedef,即使我包含了声明此类型的头文件。 这是 queue.h 中的代码:

#ifndef QUEUE_H
#define QUEUE_H

#include <stdio.h>
#include "book.h"
#include "queue_node.h"

/*
 * Queue structure for consumers threads
 */
typedef struct queue {
    pthread_mutex_t mutex;
    pthread_cond_t notempty;
    struct queue_node *rear;
} queuePtr;

void queue_enqueue(queuePtr *, orderPtr *);

orderPtr *queue_dequeue(queuePtr *queue);


#endif

这里这个类型在 book.h 中声明:

#ifndef BOOK_H
#define BOOK_H

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <pthread.h>
#include "uthash.h"
#include "customer.h"
#include <errno.h>
#include <unistd.h>
#include "queue.h"

typedef struct order {
    char *title;
    double price;
    char *idnum;
    char *category;
} orderPtr;

void order_destroy(orderPtr *);
orderPtr * order_create(char *, double, char *, char *);

#endif

enter image description here

最佳答案

如果按照include链,是这样的:

  1. main.c 包括 book.h
  2. book.h 在顶部包含 queue.h(在 orderPtr 的 typedef 之前)
  3. queue.h 尝试使用 orderPtr 尚未被 typedef'd = error
  4. 最后是 orderPtr 的 typedef。

您可以通过多种方式解决这个问题。一种方法是在 queue.h 中,您可以:

/*
 * Queue structure for consumers threads
 */
typedef struct queue {
    pthread_mutex_t mutex;
    pthread_cond_t notempty;
    struct queue_node *rear;
} queuePtr;

struct orderPtr;  // Forward declaration

// The use of struct orderPtr * is allowed because it is a pointer.
// If it wasn't a pointer, it would be an error because the size
// would be unknown.
void queue_enqueue(queuePtr *, struct orderPtr *);

struct orderPtr *queue_dequeue(queuePtr *queue);

关于c - 错误 : unknown type in C. 包含所有必需的头文件时可能是什么原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047296/

相关文章:

c - 程序在mac os下运行正常,在linux下运行异常

c strtok 从递归返回后返回 NULL

c - 在嵌入式 C 中显示 "expected expression before while"

c - C 中的 GObject 子类化无法创建子类实例

c - 为什么在 typedef 中使用指针?

c - 为什么这个缓冲区定义在一个循环内?

C变量和常量值比较不匹配

c - 尝试制作链表,出现段错误

c - C 中的 "CALLBACK"声明有什么作用?

c - OpenCl 中的结构具有大量内存地址