简体   繁体   中英

How to fix 'Size of list( a class) is unknown or zero error' and 'Declaration Syntax error'?

I tried to make a Program to Add/Delete/Display the array elements using SINGLE LINKED LIST(Stack). But three errors are showing.

I don't know to code properly (I just started in my High School this year) and I use an old version of C++.

#include<conio.h>
#include<string.h>
#include<stdio.h>
 typedef struct node
{
    char *name;
    int roll;
    struct node *next;
};

class list      //Error 1: Declaration Syntax Error
{   
    node *head;
public:
    list()
    {
    head=NULL;
    }
    void add( char *, int);
    void remove();
    void display();
};

void list::add(char *n, int r)   //Error 2 and 3: Size of list is unknown or 
                                   zero error and Declaration Syntax Error 
{
    node *p, *q;
    p=new node;
    strcpy(p->name, n);  
    p->roll=r;
    if(head==NULL)
    { 
        head=p;
        head->next=NULL;
        return;
    }
    p->next=head;
    head=p;
}

void list::remove()
{
    node *p;
    if(head==NULL)
    {
        cout<<"\n\nUnderflow";
        return;
    }
    if(head->next==NULL)
    {
        p=head;
        head=NULL;
        cout<<"\n\nElement deleted is:"<<p->name<<","<<p->roll;
        delete(p);
        return;

    }
    p=head;
    head=p->next;
    cout<<"\n\nElement deleted is"<<p->name<<","<<p->roll;
    delete(p);
}

void list::display()
{
    node *p;
    if(head==NULL)
    {
        cout<<"\n\nNothing to Display";
        return;
    }
    p=head;
    while(p->next!=NULL)
    {
        fflush(stdin);
        cout<<p->name<<" "<<p->roll<<"\n";
        p=p->next;
    }
    cout<<p->name<<" "<<p->roll;
}

void main()
{
    list X;
    char *sname;
    int ch, roll;
    clrscr();
    do
    {
    cout<<"\n\n1.Add\n\n2.Delete\n\n3.Display\n\n\nEnter your choice:";
    switch(getche())
    {
        case '1':   
            {
            cout<<"\n\n\nEnter your name:";
            gets(sname);
            getch();
            cout<<"\n\nEnter your roll:";
            cin>>roll;
            getch();
            X.add(sname,roll);
            }
            break;
        case '2':
            {
            cout<<"\n\nThe Display of your entry:";
            X.remove();
            }
            break;
        case '3':
            {
            cout<<"\n\nThe Link List elements are:\n\n";
            X.display();
            }
            break;
        default:
            cout<<"\n\nWrong choice:";
}

cout<<"\n\n\nDo you want to continue(y/n)? :";
}
while (getche()=='y');
}

It would be very helpful if someone would like to point out other errors in the code. Thank you.

It's just a guess as I cannot reproduce the error in my C++ compiler, even if I switch back to C++98 standard.

Two things:

1.) if your source file has ending .c (instead of .cpp ), then your compiler might treat your input as a C-file (and C is not aware of keyword class ).

2.) Your typedef lacks a name for the alias you define, ie it should be something like

typedef struct node
{
    char *name;
    int roll;
    struct node *next;
} node;  // <-- name for the alias.

Anyway, in C++ - in contrast to C - it is sufficient to write

struct node
{
    char *name;
    int roll;
    struct node *next;
};

in order to use both struct node or just node to refer to the struct-type.

Once you get this compiling then this code is going to give you a run time error

p=new node;
strcpy(p->name, n);  

This code copies a string to p->name . The problem is that p->name is not pointing at valid memory. In order to make this work you have to allocate some memory for the string that is being copied. This doesn't happen automatically. Something like this is needed

p = new node;
p->name = new char[strlen(n) + 1]; // allocate memory for string
strcpy(p->name, n);  

Have to agree with MSalters, whoever is teaching you this is doesn''t know much about modern C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM