【问题标题】:Declaring a structure pointer as a return type for a function将结构指针声明为函数的返回类型
【发布时间】:2017-11-29 15:22:18
【问题描述】:

以下代码给出错误-“声明语法错误” 这里节点是一个结构(对于链表) [如果这是一个愚蠢的问题,请原谅我,因为我是编程新手]

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";
}

【问题讨论】:

标签: c++


【解决方案1】:

您的主要问题是您在 main 中声明节点和 ptr。您的 enter 函数定义和其余函数将看不到 main() 中的声明。你需要移动

struct node {
    int info;
    node* next;
} * ptr, *y, *save, *start;

在 int main() 之前解决这个问题。

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多