如何修复'Size of list( a class) is unknown or zero error'和"声明语法错误"?



我试图使用单链表(堆栈)制作一个程序来添加/删除/显示数组元素。但是有三个错误正在显现。

我不知道如何正确编码(我今年刚开始在我的高中),我使用的是旧版本的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<<"nnUnderflow";
        return;
    }
    if(head->next==NULL)
    {
        p=head;
        head=NULL;
        cout<<"nnElement deleted is:"<<p->name<<","<<p->roll;
        delete(p);
        return;
    }
    p=head;
    head=p->next;
    cout<<"nnElement deleted is"<<p->name<<","<<p->roll;
    delete(p);
}
void list::display()
{
    node *p;
    if(head==NULL)
    {
        cout<<"nnNothing 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<<"nn1.Addnn2.Deletenn3.DisplaynnnEnter your choice:";
    switch(getche())
    {
        case '1':   
            {
            cout<<"nnnEnter your name:";
            gets(sname);
            getch();
            cout<<"nnEnter your roll:";
            cin>>roll;
            getch();
            X.add(sname,roll);
            }
            break;
        case '2':
            {
            cout<<"nnThe Display of your entry:";
            X.remove();
            }
            break;
        case '3':
            {
            cout<<"nnThe Link List elements are:nn";
            X.display();
            }
            break;
        default:
            cout<<"nnWrong choice:";
}
cout<<"nnnDo you want to continue(y/n)? :";
}
while (getche()=='y');
}

如果有人想指出代码中的其他错误,那将非常有帮助。谢谢。

这只是

一个猜测,因为即使我切换回 C++98 标准,我也无法在我的 C++ 编译器中重现错误。

两件事:

1.) 如果您的源文件具有结束.c(而不是 .cpp),那么您的编译器可能会将您的输入视为 C 文件(并且 C 不知道关键字 class )。

2.)您的typedef缺少您定义的别名的名称,即它应该是类似的

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

无论如何,在C++中 - 与 C 相反 - 写就足够了

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

为了同时使用struct node或仅node来引用结构类型。

一旦你得到这个编译,那么这段代码就会给你一个运行时错误

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

此代码将字符串复制到 p->name 。问题是p->name没有指向有效内存。为了完成这项工作,您必须为正在复制的字符串分配一些内存。这不会自动发生。需要这样的东西

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

不得不同意 MSalters 的观点,谁教你这个,谁对现代C++了解不多。

最新更新