怎么定义结构体子函数

tangcx 2003-10-18 04:14:36
我已经定了结构体struct student
并在 main()中定义
main()
{struct student *p,*head;
head=input(p);}
我想用子函数调用*P
是不是这样定义
struct student *input(struct student *p1)
{ 内容}
...全文
354 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
laomai 2003-10-18
  • 打赏
  • 举报
回复
struct student
{string name; /* defined data structure*/
int number;
int mark;
struct student *next;
}; //你这少了一个关键的分号,呵呵
仔细看看结构体的语法,呵呵
laomai 2003-10-18
  • 打赏
  • 举报
回复
你的结构体定义结束处应该有分号!
常见的毛病,呵呵
tangcx 2003-10-18
  • 打赏
  • 举报
回复
这种定义方 struct student *delet ( struct student *head)在C语言中肯定能通过
tangcx 2003-10-18
  • 打赏
  • 举报
回复
#include<string>
#include<iostream>
#define Null 0
using namespace std;
struct student
{string name; /* defined data structure*/
int number;
int mark;
struct student *next;
}
struct student *delet ( struct student *head) /*delete function*/
{ struct student *p,*p1=head,*p2;
p2=p1;
cout<<"input the student number what you want to delete";
cin>>p->number;
while((p1->number!=p->number)&&(p1->number!=Null))
{p2=p1;p1=p1->next;}
if(p1->number=p->number)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
}

return(head);}




main()
{struct student *p,*p1,*head,*p2;
char ch;
for(int i=1;i<=3;i++) /*input the data*/
{ p=new struct student;
cin>>p->name>>p->number>>p->mark;
if(i==1) head=p;
else p1->next=p;
p1=p;
}
p1->next=Null;
p2=head;
cout<<"do you want to insert the data(y\n)"<<endl;

cin>>ch;
if((ch=='y')||(ch=='Y')) p2=delet(head); /* call the delete function*/

while(p2!=Null) /*output the data*/
{cout<<p2->name<<p2->number<<p2->mark<<endl;
p2=p2->next;}




}
编译时:
:\C++\program\program.cpp(11) : error C2236: unexpected 'struct' 'student'
F:\C++\program\program.cpp(52) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

program.exe - 1 error(s), 1 warning(s)
我怀疑是定义函数 struct student *delete (student *head)时有错误,我的书上又没有这样的说明
气死我也
tangcx 2003-10-18
  • 打赏
  • 举报
回复
定义一个删除子函数
struct student *delet ( struct student *head) /*delete function*/
{ struct student *p,*p1=head,*p2;
p2=p1;
cout<<"input the student number what you want to delete";
cin>>p->number;
while(p1->number!=p->number)&&(p1->number!=Null)
{p2=p1;p1=p1->next;}
if(p1->number=p->number)
{if(p1==head) head=p1->next;
else p2-next=p1->next;
}

return(head);}
编译时出现:
F:\C++\program\program.cpp(11) : error C2236: unexpected 'struct' 'student'
F:\C++\program\program.cpp(11) : error C2059: syntax error : 'delete'
F:\C++\program\program.cpp(12) : error C2143: syntax error : missing ';' before '{'
F:\C++\program\program.cpp(12) : error C2447: missing function header (old-style formal list?)
F:\C++\program\program.cpp(32) : error C2143: syntax error : missing ';' before '||'
F:\C++\program\program.cpp(41) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

program.obj - 5 error(s), 1 warning(s)


darcymei 2003-10-18
  • 打赏
  • 举报
回复
nod . no problem!
晨星 2003-10-18
  • 打赏
  • 举报
回复
函数要实现什么功能?什么又叫“结构体子函数”?

69,368

社区成员

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

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