如何修复'Size of list( a class) is unknown or zero error'和"声明语法错误"?
How to fix 'Size of list( a class) is unknown or zero error' and 'Declaration Syntax 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++了解不多。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 为什么我会"error: expected expression"?
- CMake Error at modules/videoio/cmake/detect_ffmpeg.cmake:16
- 如何修复'Size of list( a class) is unknown or zero error'和"声明语法错误"?