算法搜索重复信息?

时间:2015-09-21 14:10:00

标签: c++

我正在尝试编写一个程序,该程序会提示用户他们输入的信息是重复的。但是Dev-C ++一直在告诉我'class Student'没有名为'p'的成员,所以我猜测算法或代码有问题,请看看:

Student.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

StudentList.h

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;

}

.cpp文件

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

以下是整个错误消息:

var copyObject = function(origin, keys) {
  var destination = {};
  for (var i = keys.length - 1; i >= 0; i--) {
    if(origin.hasOwnProperty(keys[i])) {
      destination[keys[i]] = origin[keys[i]];
    }
  };
  return destination;
};

var a = {
  one:"foo",
  two:"bar"
};

var schema = copyObject(a, ['two']);

3 个答案:

答案 0 :(得分: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::部分,这使它们只是全局函数,而不是成员函数。

答案 1 :(得分:1)

 private:
        Student *head, *tail;

    public:
        Student p;

你的&#39; p&#39;是StudentList类中Student类型的对象。 您在代码中所说的是获取对象&#39; p&#39;来自学生,而不是学生列表。

最重要的是,我不完全确定,但我认为你想要&#39; p&#39;是你在cpp中定义的指针

答案 2 :(得分: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初始化为未初始化的局部变量,而不是类成员。