2008下半年软考指定用书鸟哥的LINUX私房菜Linux命令、编辑器与Shell编程新蛋中国购物
发新话题
打印

help

help

#include "stdafx.h"
#include"iostream.h"
#define Null 0
#define LEN sizeof(struct student)
struct student
{                                                                                                                                                                                                                                                                                          
        int  num;
        int  score;
        struct student *next;
};
int n;
int main()
{
        int num;
        int score;
  creat();
  
  //print();
  return 0;
}


struct student *creat(void)
        {
    struct student *head;
        struct student *p1,*p2;
        n=0;
        p1=p2=(struct student *)malloc(LEN);
        scanf("%ld,%f",&p1->num,&p1->score);
        head=NULL;
        while(p1->num!=0)
        {
                n=n+1;
                if(n==1)head=p1;
                else p2->next=p1;
                p2=p1;
                p1=(struct student *)malloc(LEN);
                scanf("%d,%d",&p1->num,&p1->score);
        }
        p2->next=NULL;
        return(head);
}

/*void print(struct student *head)
{
        struct student *p;
        printf("\n Now,These %d records are:\n",n);
        p=head;
        if(head!=NULL)
                do
                {
                        printf("%ld %5.1f\n",p->num,p->score);
                        p=p->next;
                }
                while(p!=NULL);
}*/

--------------------Configuration: jianlilianbiao - Win32 Debug--------------------
Compiling...
jianlilianbiao.cpp
F:\temp\jianlilianbiao\jianlilianbiao.cpp(19) : error C2065: 'creat' : undeclared identifier
F:\temp\jianlilianbiao\jianlilianbiao.cpp(27) : error C2373: 'creat' : redefinition; different type modifiers
F:\temp\jianlilianbiao\jianlilianbiao.cpp(31) : error C2065: 'malloc' : undeclared identifier
执行 cl.exe 时出错.

jianlilianbiao.exe - 1 error(s), 0 warning(s)

回复woshiyeyongxin

F:\temp\jianlilianbiao\jianlilianbiao.cpp(19) : error C2065: 'creat' : undeclared identifier
F:\temp\jianlilianbiao\jianlilianbiao.cpp(27) : error C2373: 'creat' : redefinition; different type modifiers
F:\temp\jianlilianbiao\jianlilianbiao.cpp(31) : error C2065: 'malloc' : undeclared identifier

第一、二个错误:你的creat函数是在被 调用 处之后(main()函数之后)定义 的,因此,为了让编译器事先知道你这个函数的功能,你需要在调用之前(main()之前)加上一句关于它(creat()函数)的前向声明,即:在main()之前加上一句struct student *creat(void)
;就可以通过。
第三个错误 是因为,你调用 的 malloc()函数是在malloc.h头文件中,因此,你需要在程序前面加一句#include <malloc.h>,另外,给你一个意见,在C++中已经分别用new和delete来取代malloc()和free()因此,你如果是进行C++开发,提倡使用new和delete。




发新话题