gpt4 book ai didi

c++ - C++ 队列中的错误

转载 作者:行者123 更新时间:2023-11-28 03:19:09 25 4
gpt4 key购买 nike

我不明白这段代码有什么问题。我有几个

C2227: left of '->status' must point to class/struct/union/generic type

C2065: 'head' : undeclared identifier

C3861: 'main': identifier not found

我想访问位于队列头部的项目的状态,并在 define 中给出的 3 个状态之间更改它的值。但是我在尝试访问它时做错了一些我不知道的事情。这是代码:

#define TOTALPACKETS 100
#define WINDOW 5
#define ACK 2
#define PENDING 1
#define NEW 0


typedef int Item ;
typedef struct node *link;
struct node{
Item data;
Item status;
link next;
};

int QUEUEempty(link head){
return head==NULL;
}

void QUEUEput(link *head, link *tail, Item data, Item status){
if (*head==NULL){
(*tail)=(link)malloc(sizeof(node));
(*tail)->data=data;
(*tail)->next=NULL;
(*tail)->status=NEW;
*head=*tail;
return;}
(*tail)->next=(link)malloc(sizeof(node));
*tail=(*tail)->next;
(*tail)->data=data;
(*tail)->next=NULL;
(*tail)->status=NEW;
return;
}

Item QUEUEget(link *head){
Item data=(*head)->data;
link t=*head;
*head=(*head)->next;
free(t);
return data;
}

void send(int sPacket){
(*head)->status=PENDING;//c2227,c2065
printf("Packet No. %d: %d",sPacket,*head->status);//c2227,c2065
}

void receive(){
if ((*head)->status==PENDING || (*head)->status=NEW) {//c2227,c2065
(*head)->status=ACK; //c2227,c2065
}
}


int main() {
int i,j,k,packets=0;
link head=NULL,tail=NULL;
for(i=0;i<TOTALPACKETS;i++){
QUEUEput(&head,&tail,i,NEW);
packets++;
}
while(!QUEUEempty(head)){
for (j=0;j<WINDOW;j++){
k=TOTALPACKETS-packets;
send(k);
receive();
if ((*head).status==ACK){
printf("Packet No. %d: %d",k,*head->status);
QUEUEget(&head);
}
}
}
return 0;
}

最佳答案

您未能将 head 传递给 send()receive()

关于您的main(),参见Can the arguments of main's signature in C++ have the unsiged and const qualifiers?

关于c++ - C++ 队列中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15973132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com