高手们帮忙解决下!我新人问题!

talangren 2007-05-20 12:36:57
#define NULL 0
#define TYPE struct stu
#define LEN sizeof (struct stu)
#include <malloc.h>
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE *creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
}
在vc6.0 中编译2个报错
Compiling...
2.cpp
C:\C语言程序\2.cpp(18) : error C2065: 'printf' : undeclared identifier
C:\C语言程序\2.cpp(19) : error C2065: 'scanf' : undeclared identifier
Error executing cl.exe.

2.obj - 2 error(s), 0 warning(s)
在win-tc中编译报告没有主函数mian,请教高手们我如何在win-tc中编译通过,在vc6.0中也编译通过呢??


...全文
427 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cestchar 2007-05-23
  • 打赏
  • 举报
回复
楼主是自学的吧...要好好看书啊...
dai_weitao 2007-05-22
  • 打赏
  • 举报
回复
汗楼主了..... 这...
xinfeiniao 2007-05-22
  • 打赏
  • 举报
回复
晕,连main()都没有,以后可不能出这样的错误了
  • 打赏
  • 举报
回复
加上
int main()
{
}
talangren 2007-05-20
  • 打赏
  • 举报
回复
谢谢!加上后在VC6.0中编译是没问题了!但是不能正常运行!请教如何输出结果!
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

2.exe - 2 error(s), 0 warning(s)
运行后出现这样!在VC6.0中.
在win-tc中如何正常运行呢??显示缺少主函数main().
请教!!!!!!!!!!!!!!
ayw215 2007-05-20
  • 打赏
  • 举报
回复
然后再加上
void main()
{
...........
}
  • 打赏
  • 举报
回复
#include<stdio.h>
最前面加上
talangren 2007-05-20
  • 打赏
  • 举报
回复
谢谢!我这实际是引用C的程序!这是谭浩强老师的C语言程序设计书中关于结构体与共用体一章中链表一节中的一段原书的例题!只是我将它拷贝出来在VC6.0中编译!编译中才发现不是一段完整的程序!只是一个举例说明链表的使用!最基本的没有主函数main(),因此有了一种想把程序补充完整的想法!但实在能力有限!因此求助网上许多高手帮忙解决!这是其中一个高手给我补充的程序!!!!
谢谢bargio_susie(平和 自制 审慎) 的回复!
支持并向高手学习!!!!!
bargio_susie 2007-05-20
  • 打赏
  • 举报
回复
首先说明几点:
1.你这程序是用C写的还是C++???
如果是用C++,干吗还用什么stdio.h,scanf,printf.
如果是用C写的,
struct stu
{
int num;
int age;
stu *next;------>这里就不对了,应该是struct stu *next;
};
下面同样的道理..
下面是用C改的建立链表,打印链表的.... 如果想用C++, 你就用你原来的好了,不过那些C打印,输出语句改为C++的好了.....

#include <malloc.h>
#include <stdio.h>

#define LEN sizeof(struct stu)

struct stu
{
int num;
int age;
struct stu *next;
};

struct stu * head;
struct stu *creat(int n)
{
struct stu *pf,*pb;
int i;
for(i=0; i<n; i++)
{
pb = (struct stu *)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d", &pb->num, &pb->age);

if(i == 0)
pf=head=pb;
else
pf->next = pb;

pb->next = NULL;
pf = pb;
}

return(head);
}

void Print(struct stu *head) //打印建立的链表
{
struct stu *p = head;
while (p != NULL )
{
printf("%d\t%d\n", p->num, p->age);
p = p->next;
}
}


int main()
{
struct stu *head; //这里添加程序要完成的功能

head = creat(5);
Print(head);

return 0;
}
talangren 2007-05-20
  • 打赏
  • 举报
回复
这样改后
#define NULL 0
#define TYPE stu
#define LEN sizeof (stu)
#include <malloc.h>
#include<stdio.h>

struct stu
{
int num;
int age;
stu *next;
};


stu * head;
stu *creat(int n)
{
stu *pf,*pb;

for(int i=0;i<n;i++)
{
pb=(stu*)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",pb->num,pb->age);

if(i==0)
pf=head=pb;
else pf->next = pb;

pb->next=NULL;
pf=pb;
}

return(head);
}


int main()
{

//这里添加程序要完成的功能
return 0;
}
高手们帮忙小弟指点下!
talangren 2007-05-20
  • 打赏
  • 举报
回复
#define NULL 0
#define TYPE struct stu
#define LEN sizeof(struct stu)
#include <malloc.h>
#include<stdio.h>
void main()
{
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE *creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
}
}
这样改后还是出现抱错问题!
c:\c语言程序\2.cpp(15) : error C2601: 'creat' : local function definitions are illegal
Error executing cl.exe.

2.obj - 1 error(s), 0 warning(s)
请高手们给个完整的可运行程序好吗??不胜感激!
wanghao2979 2007-05-20
  • 打赏
  • 举报
回复
是这样的

每个程序都是以main()为入口去执行的

你不写main()
程序不执行呀~
bierhoffwang 2007-05-20
  • 打赏
  • 举报
回复
int main()
{
}

别忘了{}
bargio_susie 2007-05-20
  • 打赏
  • 举报
回复
建议还是先去好好看下书,根本就不清楚程序的流程。。。。。。。。
talangren 2007-05-20
  • 打赏
  • 举报
回复
#define NULL 0
#define TYPE struct stu
#define LEN sizeof (struct stu)
#include <malloc.h>
#include<stdio.h>
int main()
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE *creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
}
Compiling...
2.cpp
C:\C语言程序\2.cpp(8) : error C2143: syntax error : missing ';' before '<class-head>'
C:\C语言程序\2.cpp(8) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

2.exe - 2 error(s), 0 warning(s)
还是报错!请教!

33,317

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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