【发布时间】:2016-04-03 12:20:40
【问题描述】:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class student
{ int rno;
char name[20];
float total;
public:
void In();
void Out();
void search(student ob[50],int,int);
int retrno()
{ return rno; //returns the roll no of student
}
};
void student::search(student ob[],int n,int srno)
{ int flag=0;
for(int i=0;i<n;++i)
if(ob[i].retrno()==srno) //checking for rollno match
{ flag=1;
cout<<"Student found";
ob[i].Out(); //calling Out() when match is found
}
if(flag==0)
cout<<"No matching records";
}
void student::In()
{ cout<<"Enter rno:";
cin>>rno;
cout<<"Enter name:";
gets(name);
cout<<"Enter total:";
cin>>total;
}
void student::Out()
{ cout<<"rno:";
cout<<rno;
cout<<"Name:";
puts(name);
cout<<"Total:";
cout<<total;
}
void main()
{ student ob[50];
int n,srno;
cout<<"Enter no. of students:";
cin>>n;
for(int i=0;i<n;++i)
ob[i].In(); //In() is called n times
cout<<"Enter rno to be searched:";
cin>>srno;
ob.search(ob,n,srno);
}
编译时出现 2 个错误。
11: Undefined structure 'student'
52: Structure required on left side of . or .*
我试图查看函数原型是否有错误,但它看起来很好。我厌倦了没有数组大小的原型并得到了同样的错误。我也尝试在全局范围内初始化该类,但遇到了同样的错误(我没有想法了)。 请帮我找出问题所在。
【问题讨论】:
-
ob.search(ob,n,srno);你需要指定一个索引:ob[42].search(ob,n,srno); -
如果不解决您的问题,如果您有任何书推荐使用
iostream.h或conio.h,那本书可能已经过时了。混合 IOStreams 和 stdio 也不是一个好主意。获取更新的教程。 -
@πάνταῥεῖ 我现在试过了。第一个错误仍然存在。由于我在 search() 中运行 for 循环,指定索引会产生问题吗?
-
@UlrichEckhardt 请多多包涵。老师教我们这一切,这是学校教学大纲的一部分,我无法改变它。不,我不能离开新学校,因为全国各地的教学大纲都是一样的。
-
@SMcCK 你应该过度考虑你的类设计,并考虑在你的数组上实现支持
std::find()操作的东西。