2016-04-03 39 views
-2
#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 .* 

我想看看有没有函数原型一个错误,但它看起来fine.I累原型没有数组的大小,并获得同样的错误。我也尝试过在全球范围内初始化班级,但得到同样的错误(我用完了想法)。 请帮我找出问题所在。

+1

'ob.search(OB,N,srno);'你需要指定一个索引:'ob [42] .search(ob,n,srno);' –

+0

没有解决你的问题,如果你有任何书推荐使用'iostream.h'或'conio.h',书可能是可怕的过时。混合IOStreams和stdio也不是一个好主意。获取更新的教程。 –

+0

@πάνταῥεῖ我试过现在。第一个错误仍然存​​在。由于我在search()中运行for循环,会指定索引创建问题吗? – SMcCK

回答

0

student类代表一个学生,我认为这是搜索功能最好不要成为其中一员,考虑到你传递给它的学生组成的数组太:

void search(student ob[], int n, int srno) 
{ 
    // ... keep your implementation 
} 

然后你可以调用与:

search(ob,n,srno); 

这将是更好的输出部分分开,使搜索功能只返回一个索引或指针找到(或不可)的元素。

我同意Ulrich Eckhardt关于包含的库。

我想补充一点,你应该检查用户输入莫名其妙什么,我想记住,更正确的使用方法:

int main() { 
    // ... stuff... 
    return 0; 
}