2015-09-21 51 views
-1

我試圖編寫一個程序,提示用戶他們輸入的信息是重複的。但開發-C++始終告訴我「類學生」沒有名爲「P」成員,所以我猜有一些錯誤的算法或代碼,請看看:算法來搜索重複的信息?

Student.h

#ifndef STUDENT_H_ 
#define STUDENT_H_ 

#include "Person.h" 

class Student: public Person { 
    int ent_year; 
    string major; 
    public: 
     Student *next; 
     Student(); 
     Student (int i_pid, string i_fname, string i_dob, string i_addr, int i_ent_year, string i_major); 
     void Show(); 

     void Set_ent_year (int i_ent_year); 
     void Set_major(string i_major); 

     int Get_ent_year(); 
     string Get_major(); 
}; 

#endif 

StudentList.h

#ifndef STUDENTLIST_H_ 
    #define STUDENTLIST_H_ 

    #include "Student.h" 

    class StudentList { 
     private: 
      Student *head, *tail; 

     public: 
      Student p; 
      Student *next; 
      StudentList(); 

      void SList_Init(); 
      void AddTail (Student *p); 
      void SubString (string s); 
      void ListShow(); 
      void ReadFile(); 
      void findID(); 
      void findName(); 
      void findDOB(); 
      void findAddr(); 
      void findMajor(); 
      void findEY(); 
      void changeName(); 
      void changeDOB(); 
      void changeAddr(); 
      void changeMajor(); 
      void changeEY(); 
      void Add_Student(); 

      bool is_duplicate(Student t); 
    }; 

    void Open_file (string file_name); 
    void Close_file(); 

    #endif 

.cpp文件

bool equalStudent(Student s1, Student s2) 
{ 
    return (s1.Get_ent_year() == s2.Get_ent_year()) 
      && ((s1.Get_addr()).compare(s2.Get_addr()) == 0) 
      && ((s1.Get_dob()).compare(s2.Get_dob()) == 0) 
      && ((s1.Get_fname()).compare(s2.Get_fname()) == 0) 
      && ((s1.Get_major()).compare(s2.Get_major()) == 0); 
} 


bool is_duplicate(Student s1) { 
    Student *head; 
    Student *h1 = head; 
    while (h1 != NULL) { 
     if (equalStudent(h1->p, s1)) { 
      return true; 
     } 
     h1 = h1->next; 
    } 
    return false; 
} 

void StudentList:: Add_Student() 
{ 
    int new_pid, new_ent_year; 
    string new_fname, new_dob, new_addr, new_major; 
    cout << endl << "Enter student information:" << endl; 
    cout << "Full name: "; cin.ignore(1); getline (cin,new_fname); 
    cout << "Date of birth: "; getline (cin,new_dob); 
    cout << "Address: "; getline (cin,new_addr); 
    cout << "Entrance year: "; cin >> new_ent_year; 
    cout << "Major: "; cin.ignore(1); getline (cin,new_major); 

bool duplicate = is_duplicate(new_pid, new_ent_year, new_fname, new_dob, new_addr, new_major); // call function to check for duplicate info 
if (duplicate) { 
    string proceed; 
    cout << "Duplicated! Continue? Proceed? [y/n] "; cin.ignore(1); getline (cin, proceed); 
    if (proceed != "y") { 
     return; 
    } 
} 
Student *p = new Student (new_pid, new_fname, new_dob, new_addr, new_ent_year, new_major); 
AddTail (p); 

f.seekg(0, ios::end); 
f << endl << new_pid << ":" << new_fname << ":" << new_dob << ":" << new_addr << ":" << new_ent_year << ":" << new_major; 

} 

這裏是整個錯誤消息:

In function 'bool is_duplicate(Student)': 
[Error] 'class Student' has no member named 'p' 
In member function 'void StudentList::Add_Student()': 
[Error] no matching function for call to 'StudentList::is_duplicate(int&, int&, std::string&, std::string&, std::string&, std::string&)' 
[Note] candidate is: 
In file included from StudentList.cpp 
[Note] bool StudentList::is_duplicate(Student) 
[Note] candidate expects 1 argument, 6 provided 
+0

你會告訴我們'學生'類,因爲錯誤是關於它嗎? – SingerOfTheFall

+0

是的,我編輯過。 – Marco

+1

從你顯示的內容看,'Student'類沒有名爲'p'的成員。哪一行產生錯誤? – Andy

回答

1
private: 
     Student *head, *tail; 

    public: 
     Student p; 

你的 'P' 是你StudentList類類型學生的對象。 你在代碼中說的是從Student而不是StudentList中獲得對象'p'。

最重要的是,我不是完全確定,但我想你想「P」是你在你的CPP定義指針後

2

我想你應該改變你的is_duplicate

bool StudentList::is_duplicate(Student s1) { //this is a class member function, hence the StudentList:: 
    Student *h1 = head; //starting with the head of the list, just 1 variable is enough to iterate 
    while (h1 != NULL) { 
     //comparing current student with s1 
     if (equalStudent(*h1, s1)) { 
      return true; 
     } 
     h1 = h1->next; 
    } 
    return false; 
} 

還要注意你的函數定義。雖然你的Add_Student被定義爲正確(void StudentList::Add_Student()),但其餘功能缺少StudentList::部分,這使得它們只是全局函數,而不是成員函數。

+0

我實際上已經試過了,它說「頭部沒有在這個範圍內聲明」,我想我已經宣佈了它。 – Marco

+0

@Marco,那是因爲你的'is_duplicate'只是一個全局函數。爲了提供作爲類的一部分的函數的定義,它應該看起來像'return_type ClassName :: functionName(<...>){<...>}'。你也應該這樣修復其他功能。 – SingerOfTheFall

0
bool is_duplicate(Student s1) { 
    Student *head; 
    Student *h1 = head; 
    while (h1 != NULL) { 
     if (equalStudent(h1->p, s1)) { 
      return true; 
     } 
     h1 = h1->next; 
    } 
    return false; 
} 

問題:通過在方法的範圍內聲明的副本Student *head;Student *h1 = head;是要初始化H1到未初始化的局部變量,而不是類成員。