如何在链表中搜索结构对象
How to search a linked list for a structure object
对于这个程序,我不允许使用向量或 std::。
我有一个名为 StudentList.cpp
的程序,它使用链表 class 和一个存储文件中学生信息的结构。 removeStudent
函数要求用户输入学号。我构建了一个临时 Student 对象并在链表中搜索 ID 号。如果找到学生,那么我会显示学生的全部记录并删除学生。
这是我遇到问题的地方。在 removeStudent
函数中,我无法正确调用删除函数。 当我从 LinkedList.h
调用 remove
函数时,我收到以下错误消息:
error message: a reference type "Student &" (non const-quaified) cannot be initialized with value type "int"
以下是我在 StudentList.cpp
程序中遇到问题的功能:
void removeStudent(LinkedList<Student> &list)
{
int id;
cout << "Please enter student ID number: ";
cin >> id;
Student temp; //structure Student temporary object
if (list.remove(temp.id) == false) //error message: a reference type "Student &" (non const-quaified) cannot be initialized with value type "int"
cout << "Student not found.\n";
else
{
cout << id << endl;
cout << "Student removed.\n";
}
}
我有一个名为 LinkedList.h
的程序,其中包含使用链表的函数。我知道这个程序工作得很好。我从 removeStudent
调用的函数是:
template <class TYPE>
bool LinkedList<TYPE>::remove(TYPE &dataOut)
{
bool success = false;
Node<TYPE> *pTemp = front;
Node<TYPE> *pPrev = nullptr;
while (pTemp != nullptr && pTemp->data < dataOut)
{
pPrev = pTemp;
pTemp = pTemp->next;
}
if (pTemp != nullptr && pTemp->data == dataOut)
{
dataOut = pTemp->data;
if (pPrev != nullptr)
pPrev->next = pTemp->next;
else
front = pTemp->next;
delete pTemp;
success = true;
}
return success;
}
感谢您的更新。您正在调用一个需要一种数据类型的函数,但向它传递了一种不同的数据类型。编译器正确地拒绝了这个,因为你给了它错误的数据类型。
template<class TYPE>
class LinkedList {
public:
bool remove(TYPE& x);
};
template <class TYPE>
bool LinkedList<TYPE>::remove(TYPE& dataOut)
{
return false;
}
struct Student { int id; };
int main() {
LinkedList<Student> list;
int id = 1;
list.remove(id); // reproduced error here
Student temp{ id };
list.remove(temp); // correct here
}
该代码出现此错误:
error C2664: 'bool LinkedList::remove(TYPE &)': cannot convert argument 1 from 'int' to 'TYPE &'
如果我注释掉错误的行,代码将编译并运行。请注意,下一行使用 Student.
实例调用 list.remove
错误的确切文本会因编译器而异,但本质上您向它传递了一个整数,但它期望引用一个 Student object。
(你也没有初始化那个变量,但这是一个完全不同的问题)
由于 class 它是一个模板 class,当您查看原型“bool LinkedList::remove(TYPE& dataOut)”时,您知道它必须是某种类型未在模板中指定 class。其实是在这里决定的:
LinkedList<Student> list;
^^^^^^^
TYPE 是 Student,但它是一个模板并不重要。这只是让阅读 header 和知道函数期望的类型变得有点困难。
对于这个程序,我不允许使用向量或 std::。
我有一个名为 StudentList.cpp
的程序,它使用链表 class 和一个存储文件中学生信息的结构。 removeStudent
函数要求用户输入学号。我构建了一个临时 Student 对象并在链表中搜索 ID 号。如果找到学生,那么我会显示学生的全部记录并删除学生。
这是我遇到问题的地方。在 removeStudent
函数中,我无法正确调用删除函数。 当我从 LinkedList.h
调用 remove
函数时,我收到以下错误消息:
error message: a reference type "Student &" (non const-quaified) cannot be initialized with value type "int"
以下是我在 StudentList.cpp
程序中遇到问题的功能:
void removeStudent(LinkedList<Student> &list)
{
int id;
cout << "Please enter student ID number: ";
cin >> id;
Student temp; //structure Student temporary object
if (list.remove(temp.id) == false) //error message: a reference type "Student &" (non const-quaified) cannot be initialized with value type "int"
cout << "Student not found.\n";
else
{
cout << id << endl;
cout << "Student removed.\n";
}
}
我有一个名为 LinkedList.h
的程序,其中包含使用链表的函数。我知道这个程序工作得很好。我从 removeStudent
调用的函数是:
template <class TYPE>
bool LinkedList<TYPE>::remove(TYPE &dataOut)
{
bool success = false;
Node<TYPE> *pTemp = front;
Node<TYPE> *pPrev = nullptr;
while (pTemp != nullptr && pTemp->data < dataOut)
{
pPrev = pTemp;
pTemp = pTemp->next;
}
if (pTemp != nullptr && pTemp->data == dataOut)
{
dataOut = pTemp->data;
if (pPrev != nullptr)
pPrev->next = pTemp->next;
else
front = pTemp->next;
delete pTemp;
success = true;
}
return success;
}
感谢您的更新。您正在调用一个需要一种数据类型的函数,但向它传递了一种不同的数据类型。编译器正确地拒绝了这个,因为你给了它错误的数据类型。
template<class TYPE>
class LinkedList {
public:
bool remove(TYPE& x);
};
template <class TYPE>
bool LinkedList<TYPE>::remove(TYPE& dataOut)
{
return false;
}
struct Student { int id; };
int main() {
LinkedList<Student> list;
int id = 1;
list.remove(id); // reproduced error here
Student temp{ id };
list.remove(temp); // correct here
}
该代码出现此错误:
error C2664: 'bool LinkedList::remove(TYPE &)': cannot convert argument 1 from 'int' to 'TYPE &'
如果我注释掉错误的行,代码将编译并运行。请注意,下一行使用 Student.
实例调用 list.remove错误的确切文本会因编译器而异,但本质上您向它传递了一个整数,但它期望引用一个 Student object。
(你也没有初始化那个变量,但这是一个完全不同的问题)
由于 class 它是一个模板 class,当您查看原型“bool LinkedList::remove(TYPE& dataOut)”时,您知道它必须是某种类型未在模板中指定 class。其实是在这里决定的:
LinkedList<Student> list;
^^^^^^^
TYPE 是 Student,但它是一个模板并不重要。这只是让阅读 header 和知道函数期望的类型变得有点困难。