| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1303 人关注过本帖
标题:一个链表问题,弹出许多非法定义错误
只看楼主 加入收藏
jioper
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2016-10-21
结帖率:70%
收藏
已结贴  问题点数:10 回复次数:1 
一个链表问题,弹出许多非法定义错误
将如表21.1所示的学生信息建立成一个按学号升序有序的链表,每个节点包括学号、姓名、性别、年龄。统计链表中年龄小于20的学生个体数并将对应的学生信息(包括学号、姓名、性别、年龄)输出
代码如下:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void main()
{
    struct student
    {
        int id;
        char name[10];
        char x;
        int age;
        struct student *next;
    };
    struct student *head,*tail;
    void lianbiao(struct student *stu)
    {//第16行
        stu=(struct student *)malloc(sizeof(struct student));
        head=tail=stu;
        stu->next=NULL;
    }
    void jialianbiao(int idd,char *namee,char xx,int agee)
    {//22行
        struct student *ss=(struct student *)malloc(sizeof(struce student));
        ss->id=idd
            strcpy(ss->name,namee);
        ss->x=xx;
        ss->age=agee;
        tail->next=ss;
        ss->next=NULL;
        tail=ss;
    }
    void tlianbiao()
    {//33行
        struct student *now;
        now=head->next;
        while(now!=NULL)
        {
            if(now->age<20)
                printf("%5d%5s%5c%5d\n",now->id,now->name,now->x,now->age);
            now=now->next;
        }
    }
    void destroy()
    {//44行
        struct student *now;
        now=head;
        while(now!=NULL)
        {
            head=now->next;
            free(now);
            now=head;
        }
    }
    int main()
    {//55行
    struct student *stu;
    liaobiao(stu);
    jialianbiao(305001,"Zhang",'M',18);
    jialianbiao(305002,"Wang",'F',20);
    jialianbiao(305003,"Li",'F',19);
    jialianbiao(305004,"Zhao",'M',21);
    printf("学号     姓名     性别     年龄\n");
    tlianbiao();
    destroy();
    return 0;
    }
}
有如下错误
C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(16) : error C2601: 'lianbiao' : local function definitions are illegal
C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(22) : error C2601: 'jialianbiao' : local function definitions are illegal
C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(33) : error C2601: 'tlianbiao' : local function definitions are illegal
C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(44) : error C2601: 'destroy' : local function definitions are illegal
C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(55) : error C2556: 'int __cdecl main(void)' : overloaded function differs only by return type from 'void __cdecl main(void)'
        C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(4) : see declaration of 'main'
C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(55) : error C2371: 'main' : redefinition; different basic types
        C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(4) : see declaration of 'main'
C:\Users\Administrator\Desktop\新建文件夹\链表.cpp(55) : error C2601: 'main' : local function definitions are illegal
执行 cl.exe 时出错.


为什么错了?明明定义没啥问题啊?
搜索更多相关主题的帖子: include 统计 信息 姓名 
2016-12-04 15:35
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43182
注 册:2014-5-20
收藏
得分:10 
笔误:
void jialianbiao(int idd,char *namee,char xx,int agee)
{
    ......

    //struct student *ss=(struct student *)malloc(sizeof(struce student));
    struct student *ss=(struct student *)malloc(sizeof(struct student));
    //ss->id=idd
    ss->id=idd;
    ......
}

int main()
{
    ......
    //liaobiao(stu);
    lianbiao(stu);
    ......
}

代码结构改为:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"

struct student
{
    int id;
    char name[10];
    char x;
    int age;
    struct student *next;
};

struct student *head,*tail;

void lianbiao(struct student *stu)
{
    //第16行
    stu=(struct student *)malloc(sizeof(struct student));
    head=tail=stu;
    stu->next=NULL;
}

void jialianbiao(int idd,char *namee,char xx,int agee)
{
    //22行
    //struct student *ss=(struct student *)malloc(sizeof(struce student));
    struct student *ss=(struct student *)malloc(sizeof(struct student));
    //ss->id=idd
    ss->id=idd;
    strcpy(ss->name,namee);
    ss->x=xx;
    ss->age=agee;
    tail->next=ss;
    ss->next=NULL;
    tail=ss;
}

void tlianbiao()
{
    //33行
    struct student *now;
    now=head->next;
    while(now!=NULL)
    {
        if(now->age<20)
            printf("%5d%5s%5c%5d\n",now->id,now->name,now->x,now->age);
        now=now->next;
    }
}

void destroy()
{
    //44行
    struct student *now;
    now=head;
    while(now!=NULL)
    {
        head=now->next;
        free(now);
        now=head;
    }
}

int main()
{
    //55行
    struct student *stu;
    //liaobiao(stu);
    lianbiao(stu);
    jialianbiao(305001,"Zhang",'M',18);
    jialianbiao(305002,"Wang",'F',20);
    jialianbiao(305003,"Li",'F',19);
    jialianbiao(305004,"Zhao",'M',21);
    printf("学号     姓名     性别     年龄\n");
    tlianbiao();
    destroy();
    return 0;
}

2016-12-04 16:37
快速回复:一个链表问题,弹出许多非法定义错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019431 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved