如何运行一个多文件程序?

高维橘子 2010-12-01 02:34:38
下面的代码是一个主函数文件和四个被调用的函数文件,为什么连接起来以后出现问题呢?编译是对的。
提示如下:
--------------------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);
}

...全文
259 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
高维橘子 2010-12-02
  • 打赏
  • 举报
回复
你说的很有用,只是还有一个问题,我自己再问问别人吧。谢谢你了!
[Quote=引用 15 楼 cjf_iceking 的回复:]
1.一般将全局变量,数据结构,函数原型等 放在.h文件中
.cpp主要写函数实现等

2.每个cpp单独编译,连接器链接他们所生成的.obj。

3.如果你一个cpp文件里需要用到另一个cpp里的变量:
比如
a.cpp
int a;

b.cpp需要用到a,必须 extern int a;
[/Quote]
高维橘子 2010-12-01
  • 打赏
  • 举报
回复
明天上班的时候试试,谢谢你的回答。

[Quote=引用 15 楼 cjf_iceking 的回复:]

1.一般将全局变量,数据结构,函数原型等 放在.h文件中
.cpp主要写函数实现等

2.每个cpp单独编译,连接器链接他们所生成的.obj。

3.如果你一个cpp文件里需要用到另一个cpp里的变量:
比如
a.cpp
int a;

b.cpp需要用到a,必须 extern int a;
[/Quote]
  • 打赏
  • 举报
回复
1.一般将全局变量,数据结构,函数原型等 放在.h文件中
.cpp主要写函数实现等

2.每个cpp单独编译,连接器链接他们所生成的.obj。

3.如果你一个cpp文件里需要用到另一个cpp里的变量:
比如
a.cpp
int a;

b.cpp需要用到a,必须 extern int a;

xspace_time 2010-12-01
  • 打赏
  • 举报
回复
xxx.cpp没错,没问题,就是.txt都没问题,在c中直接包含.c就可以了
你可以尝试把student结构体和n的定义放入main函数中,或者在没有定义student结构体而引用了student的文件语句之前加上extern
jinlin1989 2010-12-01
  • 打赏
  • 举报
回复
学习。。。。。#include "xxx.cpp"。。。。。。。
高维橘子 2010-12-01
  • 打赏
  • 举报
回复
那怎么引用别的文件中的函数呢,头文件中可以定义函数吗?
[Quote=引用 10 楼 zhao4zhong1 的回复:]
一般只
#include "xxx.h"

#include "xxx.cpp"
[/Quote]
wyfwx 2010-12-01
  • 打赏
  • 举报
回复
#include "ex0701.cpp"
#include "ex0702.cpp"
#include "ex0703.cpp"
#include "ex0704.cpp"


你自己发明的,但是c不认
赵4老师 2010-12-01
  • 打赏
  • 举报
回复
一般只
#include "xxx.h"

#include "xxx.cpp"
高维橘子 2010-12-01
  • 打赏
  • 举报
回复
哎呀,你们倒是说说为什么“情何以堪”嘛。
[Quote=引用 8 楼 zhaoli_1956 的回复:]
引用 3 楼 chen200928014825041 的回复:

请问“XXX.cpp”是有什么问题吗?
引用 1 楼 milkylove 的回复:

又见#include "XXX.cpp"……这叫我情何以堪……


同样情何以堪......
[/Quote]
zhaoli_1956 2010-12-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chen200928014825041 的回复:]

请问“XXX.cpp”是有什么问题吗?
引用 1 楼 milkylove 的回复:

又见#include "XXX.cpp"……这叫我情何以堪……
[/Quote]

同样情何以堪......
高维橘子 2010-12-01
  • 打赏
  • 举报
回复
compile(编译)没有错误,build(连接)出现的那些问题。
[Quote=引用 4 楼 winton_adlsy 的回复:]

编译不是错了吗
[/Quote]
高维橘子 2010-12-01
  • 打赏
  • 举报
回复
主函数是写在ex0705.cpp的文件中,引用的函数都是01、02、03、04中的函数。书上说函数没有写static就是可以外部调用的,而且在主函数之前写了include命令,编译时就把那几个文件自动放在main函数的前面,作为一个整体进行编译,这些函数就被认为是在同一个文件中了。
[Quote=引用 5 楼 q191201771 的回复:]

你02 03 04怎么知道01里面的东西呢
[/Quote]
就想叫yoko 2010-12-01
  • 打赏
  • 举报
回复
你02 03 04怎么知道01里面的东西呢
winton_adlsy 2010-12-01
  • 打赏
  • 举报
回复
编译不是错了吗
高维橘子 2010-12-01
  • 打赏
  • 举报
回复
请问“XXX.cpp”是有什么问题吗?
[Quote=引用 1 楼 milkylove 的回复:]

又见#include "XXX.cpp"……这叫我情何以堪……
[/Quote]
luciferisnotsatan 2010-12-01
  • 打赏
  • 举报
回复
你就在ex0701.cpp里定义了全局的n,其他cpp里没有声明n
其他cpp里,在开始处用 extern int n;声明下

struct student
{
long num;
float score;
struct student *next;
};
把这个放到头文件.h里去,然后cpp里include 这个头文件
失落的凡凡 2010-12-01
  • 打赏
  • 举报
回复
又见#include "XXX.cpp"……这叫我情何以堪……

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧