[英]C Unknown type name 'DATA'
我真的不明白為什么會收到此錯誤:未知類型名稱'DATA'。
#include <stdio.h>
#include <stdlib.h>
typedef int DATA;
#include "PriorityQueue.h"
int main(){
int priority;
DATA value;
LINK *head;
createEmpty(&head);
add(&head, 1, 7);
checkIfEmpty(head);
add(&head, 5, 5);
add(&head, 3, 4);
deleteElement(&head, &priority, &value);
add(&head, 1, 3);
destroy(head);
createEmpty(&head);
add(&head, 10, 10);
return 0;
}
主要
#include <stdio.h>
#include <stdlib.h>
#include "PriorityQueue.h"
void add(LINK **head, int p, DATA v){
LINK *curr=(*head);
LINK *beforeCurr=(*head);
LINK *newElement=NULL;
if((*head)->priority==-1){
(*head)->priority=p;
(*head)->value=v;
}
else{
newElement=(LINK*)malloc(sizeof(LINK));
newElement->priority=p;
newElement->value=v;
newElement->next=NULL;
while(p>(curr->priority) && curr->next!=NULL){
beforeCurr=curr;
curr=curr->next;
}
if(curr->next==NULL && p>(curr->priority))
curr->next=newElement;
else if(curr==(*head)){
newElement->next=curr;
(*head)=newElement;
}
else{
beforeCurr->next=newElement;
newElement->next=curr;
}
}
}
void deleteElement(LINK **head, int *p, DATA *v){
LINK *curr=(*head);
LINK *currB=(*head);
while(curr->next!=NULL) {
currB=curr;
curr=curr->next;
}
currB->next=NULL;
(*p)=curr->priority;
(*v)=curr->value;
free(curr);
}
void checkIfEmpty(LINK *head){
if(head->priority==-1)
printf("Queue is empty\n\n");
else
printf("Queue is not empty\n\n");
}
void destroy(LINK *head) {
LINK *current;
LINK *next;
current=head;
while(current!=NULL){
next=current->next;
free(current);
current=next;
}
head=NULL;
}
void createEmpty(LINK **head) {
LINK *curr=NULL;
curr=(LINK*)malloc(sizeof(LINK));
curr->next = NULL;
curr->priority = -1;
(*head)=curr;
}
功能文件
#ifndef PRIORITY_QUEUE_H_
#define PRIORITY_QUEUE_H_
typedef struct priorityQueue{
int priority;
DATA value;
struct priorityQueue *next;
}LINK;
void createEmpty(LINK **head);
void add(LINK **head, int p, DATA v);
void deleteElement(LINK **head, int *p, DATA *v);
void checkIfEmpty(LINK *head);
void destroy(LINK *head);
#endif
頭
我在代碼塊上使用項目進行編譯。 另外,“ typedef int DATA;” 主要是首選
typedef
關鍵字遵循范圍規則,如下所述: typedef和#define在c中是否相同?
這意味着如果您聲明:
typedef int DATA;
並且此行不包含在另一個文件中,因此不會在那里聲明。
應該在使用它的每個文件中聲明DATA
,方法是包括另一個聲明它的文件,或者通過顯式聲明它。
您應該聲明typedef int DATA;
在頭文件中,並將該頭文件包含在所有源文件中。
當前,其他源文件無法知道什么是DATA
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.