【发布时间】:2016-04-18 17:57:57
【问题描述】:
我正在尝试制作一个从队列中弹出元素的函数
/**
* This function extracts from the queue the patient with maximum priority
* @param queue is the extraction point
* @return the patient with maximum priority or a Patient type value with all the fields 0 if the queue is empty
*/
struct Patient priorityQueuePop(struct PriorityQueue *queue);
结构 PriorityQueue 是这样的:
struct PriorityQueue{
unsigned size;
struct PriorityQueue *next;
struct PriorityQueue *front;
struct PriorityQueue *rear;
}
病人是这样的:
enum Gender {
MALE = 'M',
FEMALE = 'F'
};
struct Patient {
char firstName[20];
char lastName[20];
unsigned char age;
enum Gender gender;
};
我试着做这样的事情:
struct Patient priorityQueuePop(struct PriorityQueue *queue){
struct Patient *item =(struct Patient*)malloc(sizeof(struct Patient));
queue->front = queue->front->next;
queue->size--;
return item;
}
但是我编译的时候报错:
priorityQueue.c:72:2: error: incompatible types when returning type ‘struct Patient *’ but ‘struct Patient’ was expectedreturn item;
有人能解释一下这应该怎么做吗? 谢谢
【问题讨论】:
-
问题出在哪里?
-
您的结构看起来不正确。
PriorityQueue结构体包含指向每个节点头尾的指针,PriorityQueue和Patient结构体之间没有关系。 -
错误信息不言自明:您的
priorityQueuePop函数声明它返回struct Patient,但您返回的item类型为struct Patient *。 -
你知道数据指针和数据本身的区别吗?如果没有,您可以先学习 C 基础知识。
-
应该是
struct Patient* priorityQueuePop()