C++ 搜索重复信息的算法?
我试图编写一个程序,提示用户他们输入的信息是重复的。但是Dev-C++一直告诉我'class Student'没有名为'p'的成员,所以我猜算法或代码有问题,请看一下: 学生C++ 搜索重复信息的算法?,c++,C++,我试图编写一个程序,提示用户他们输入的信息是重复的。但是Dev-C++一直告诉我'class Student'没有名为'p'的成员,所以我猜算法或代码有问题,请看一下: 学生 #ifndef STUDENT_H_ #define STUDENT_H_ #include "Person.h" class Student: public Person { int ent_year; string major; public: Student *next;
#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
学生名单
#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;
}
您的“p”是StudentList类中Student类型的对象。
您在代码中所说的是从Student而不是StudentList获取对象“p”
除此之外,我不完全确定,但我认为您希望“p”成为您稍后在cpp中定义的指针我认为您应该将您的
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\u Student
定义正确(void StudentList::Add\u Student()
),但其余函数缺少StudentList::
部分,这使得它们只是全局函数,而不是成员函数
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;
}
问题:通过声明重复的
学生*头方法范围内的代码>你的学生*h1=头部代码>将把h1初始化为未初始化的局部变量,而不是类成员 您能给我们看一下学生
类吗,因为这个错误与此有关?从您所展示的内容来看,学生
类没有名为p
的成员。哪一行产生了错误?嗯,它确实没有任何p
成员。p
在StudentList
中,而不是Student
中。它不应该是equalStudent(*h1,s1)
而不是equalStudent(h1->p,s1)
?请尝试移除Student*头部中的code>是重复的
函数,并将h1->p
更改为*h1
@NeilKirk,虽然你有一个观点,但为了学习,尝试自己实现最基本的数据结构也是很有用的。还有,这可能是一个家庭作业或作业。实际上我已经试过了,它说“head不在这个范围内声明”,我以为我已经声明了。@Marco,那是因为你的是重复的只是一个全局函数。要提供作为类一部分的函数的定义,它应该类似于return\u type ClassName::functionName(){}
。您也应该这样修复其他函数。
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;
}
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;
}