2016-04-03 92 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; 
}