所以我无法弄清楚以下问题: 我得到 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
最佳答案
如果按照include链,是这样的:
main.c
包括book.h
book.h
在顶部包含queue.h
(在orderPtr
的 typedef 之前)queue.h
尝试使用orderPtr
尚未被 typedef'd = error- 最后是
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/