所以我无法弄清楚以下问题: 即使我已经包含声明此类型的头文件,我也会获得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
答案 0 :(得分:3)
如果您遵循包含链,就像这样:
main.c
包含book.h
book.h
在顶部包含queue.h
(在orderPtr
的typedef之前)queue.h
尝试使用尚未定义的orderPtr
=错误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);