将结构指针声明为函数的返回类型
Declaring a structure pointer as a return type for a function
以下代码给出了错误 - "声明语法错误" 这里的节点是一个结构(对于链接列表)[如果我是编程的新手,请原谅我,如果我是一个愚蠢的问题]
node * enter(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
完整代码 -
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
struct node {
int info;
node* next;
} * ptr, *y, *save, *start;
void insert(node*);
node* enter(int);
void display(node*);
start = NULL;
int inf;
cout << "Enter INFO:";
cin >> inf;
y = enter(inf);
insert(y);
cout << "display:n";
display(start);
getch();
}
node* enter(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
void insert(int* m)
{
if (start == NULL) {
start = m;
}
else {
save = start;
start = m;
m->next = save;
}
}
void display(node* l)
{
while (l != NULL) {
cout << l->info << "->";
l = l->next;
}
cout << "nOVER";
}
您的主要问题是您在main内声明节点和ptr。您对Enter和其余功能的功能定义将不会看到Main()内的声明。您需要移动
struct node {
int info;
node* next;
} * ptr, *y, *save, *start;
到int main()修复此问题。
相关文章:
- 检查函数返回类型是否与STL容器类型值相同
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 在 c++ 中将函数返回类型指定为模板参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 如何避免模板函数返回类型重复?
- 从类型bankAccount的返回值到函数返回类型int没有可行的转换
- 为什么在某些情况下从函数返回类型中删除 cv 限定符?
- 模板类内模板类的函数返回类型
- 为什么函数返回类型中不允许参数推导?
- 函数返回类型之前的"define"
- C++推断要隐式调用的模板函数返回类型
- 具有不同模板参数的函数返回类型
- 当函数返回类型为父类时,如何返回子类的对象?
- C++:在原型中声明"auto"函数返回类型仍然会导致在扣除错误之前使用"auto&quo
- C++14 'auto'能够获取函数返回类型,我们还需要 std::result_of<> 吗?
- 函数返回类型中的模板类型推断
- 嵌套模板类返回类型在 C++ 中的头文件中函数返回类型的语法
- C++重写 void 函数返回类型会导致生成失败
- 从函数内部推断函数返回类型
- C++嵌套类函数返回类型和命名空间