下面的代码是一个主函数文件和四个被调用的函数文件,为什么连接起来以后出现问题呢?编译是对的。
提示如下:
--------------------Configuration: ex0701 - Win32 Debug--------------------
Compiling...
ex0702.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(7) : error C2065: 'n' : undeclared identifier
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(12) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(4) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(12) : error C2227: left of '->num' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(12) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(4) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(12) : error C2227: left of '->score' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(13) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(4) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0702.cpp(13) : error C2227: left of '->next' must point to class/struct/union
ex0703.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0703.cpp(12) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0703.cpp(4) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0703.cpp(12) : error C2227: left of '->num' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0703.cpp(12) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0703.cpp(4) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0703.cpp(12) : error C2227: left of '->next' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0703.cpp(12) : fatal error C1903: unable to recover from previous error(s); stopping compilation
ex0704.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(11) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(5) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(11) : error C2227: left of '->next' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(14) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(5) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(14) : error C2227: left of '->num' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(14) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(5) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(14) : error C2227: left of '->num' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(14) : error C2027: use of undefined type 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(5) : see declaration of 'student'
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(14) : error C2227: left of '->next' must point to class/struct/union
D:\Program Files\Microsoft Visual Studio\MyProjects\ex0701\ex0704.cpp(14) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.
ex0701.exe - 1 error(s), 0 warning(s)
/*主函数*/
#include <stdio.h>
#include "ex0701.cpp"
#include "ex0702.cpp"
#include "ex0703.cpp"
#include "ex0704.cpp"
int main(void)
{
struct student *head, stu;
long del_num;
printf("Input records:\n");
head=creat(); /*返回头指针*/
print(head); /*输出全部结点*/
printf("\nInput the delete number:");
scanf("%ld", &del_num); /*输入要删除的学号*/
head=del(head, del_num); /*删除后链表的头地址*/
print(head); /*输出全部结点*/
printf("\nInput the inserted record:\n"); /*输入要插入的结点*/
scanf("%ld,%f", &stu.num, &stu.score);
head=insert(head, &stu); /*返回地址*/
print(head);
return 0;
}
/*ex0701.cpp*/
#include <malloc.h>
#include <stdio.h>
#define LEN sizeof(struct student)
#define NULL 0
struct student
{
long num;
float score;
struct student *next;
}; /*定义结构体类型*/
int n; /*设n为全局变量*/
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);/*malloc函数带回指针类型为void,强制转换为结构体类型*/
scanf("%ld, %f", &p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
/*ex0702.cpp*/
#include <stdio.h>
void print(struct student *head)
{
struct student *p;
printf("\nNow, 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);
}
/*ex0703.cpp*/
#include <stdio.h>
struct student *del(struct student *head, long num)
{
struct student *p1, *p2;
if (head==NULL)
{printf("\nlist null! \n"); return(head);}
else
{
p1=head;
while (num!=p1->num && p1->next!=NULL) /*p1指向的不是要找的结点,而且后面还有结点*/
{
p2=p1; p1=p1->next; /*p1后移一个结点*/
}
if (num==p1->num) /*找到了*/
{
if (p1==head) head=p1->next;/*若p1指向的是首结点,把第二个结点的地址赋给head*/
else p2->next=p1->next;/*否则将下一结点地址赋给前一结点*/
printf("delete:%ld\n", num);
n=n-1;
}
else printf("%ld not been found!\n", num);/*找不到该结点*/
}
return (head);
}
/*ex0704.cpp*/
/*对链表的插入操作*/
#include <stdio.h>
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0, *p1, *p2;
p1=head;
p0=stud;
if (head==NULL)
{head=p0; p0->next=NULL;}
else
{
while ((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if (p0->num<=p1->num)
{if (head==p1) head=p0;/*插到原来第一个结点之前*/
else p2->next = p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0; p0->next=NULL;} /*插到之后的结点之后*/
}
n=n+1;
return (head);
}