0

I can't understand what's wrong with this code. I get a couple of

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

C2065: 'head' : undeclared identifier

and

C3861: 'main': identifier not found

I want to access the status of the item located in queue's head and change its value between the 3 status given in define.But I am doing something wrong while trying to access it that I have no idea about. Here's the code :

#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;
 }
Community
  • 1
  • 1
user2192519
  • 41
  • 1
  • 1
  • 6
  • 1
    With your `malloc` and other things. this really looks more like `C` than `C++`. – crashmstr Apr 12 '13 at 13:54
  • Isn't -> for pointers, while the (*tail) actually dereferences the pointer? – Mads Apr 12 '13 at 13:55
  • To improve your question, it's necessary to include line numbers with the failures that your report. Without them, potential debuggers will have a large hill to climb simply finding your bugs. – KevinDTimm Apr 12 '13 at 13:59
  • Was trying to figure how to do it since it has no number lines here. Added them as comments. – user2192519 Apr 12 '13 at 14:04
  • 1
    It's not nice to just replace your question with a new one once you've got it answered. The posted answers are now completely irrelevant. Please post a new question if you want to ask something different. – molbdnilo Apr 12 '13 at 14:28
  • I changed it back to what it is.Didn't think of that. – user2192519 Apr 12 '13 at 14:35

2 Answers2

2

You are failing to pass head into send() and receive().

As to your main(), see Can the arguments of main's signature in C++ have the unsiged and const qualifiers?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

main have a predefined form. You can use:

int main ()  /* or: int main(int argc, char* argv[]) */  {

also:

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

...
   link head=nullptr,tail=nullptr;
...
   if (head->status==ACK){
        printf("Packet No. %d: %d",k, head->status); 


}

head is a link, a pointer to node. Also, you forgot to pass head to send and receive.

void send(int sPacket,link head ){

use send(sPacket,head)

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
  • Ok I changed it in send() and receive(). Now I get 6 errors C2228: left of '.status' must have class/struct/union in both lines of send() and receive() and in the if() in main(). – user2192519 Apr 12 '13 at 14:16
  • Good. I hope you got the idea: pointer->member .And: object.member ? – qPCR4vir Apr 12 '13 at 14:18
  • If I understood it right, pointer->member is a pointer to the member while object.member is the member itself right? – user2192519 Apr 12 '13 at 14:40