我正在尝试实现一些函数来操作链表。实现是一个模板typename T,类是'List',它包含一个'head'指针和一个struct:
struct Node { // the node in a linked list
T* data; // pointer to actual data, operations in T
Node* next; // pointer to a Node
};
由于它是一个模板,并且'T'可以是任何数据,我如何检查列表的数据以查看它是否与输入到函数中的数据匹配?
该函数称为'retrieve',它接受两个参数:数据和指针:
bool retrieve(T target, T*& ptr); // This is the prototype we need to use for the project
“bool retrieve:类似于删除,但不从列表中删除。如果列表中有重复项,则会检索遇到的第一个参数。如果返回值为false,则第二个参数不可靠。例如,”
Employee target("duck", "donald");
success = company1.retrieve(target, oneEmployee);
if (success) { cout << "Found in list: " << *oneEmployee << endl; }
这个函数就像这样调用:
company4.retrieve(emp3, oneEmployee)
因此当你cout * oneEmployee时,你将获得该指针的数据(在这种情况下,数据是Employee类型)。
(另外,假设所有数据类型都有适当的重载运算符)
我希望到目前为止这是有道理的,但我的问题是在浏览列表时比较参数和数据中的数据。 (我们使用的数据类型都包含相等运算符的重载,因此oneData == twoData有效)
这是我到目前为止所做的:
template <typename T>
bool List<T>::retrieve(T target , T*& ptr) {
List<T>::Node* dummyPtr = head; // point dummy pointer to what the list's head points to
for(;;) {
if (*dummyPtr->data == target) { // EDIT: it now compiles, but it breaks here and I get an Access Violation Reading Access error.
ptr = dummyPtr->data; // set the parameter pointer to the dummy pointer
return true; // return true
} else {
dummyPtr = dummyPtr->next; // else, move to the next data node
}
}
return false;
}
以下是Employee类的实现:
//-------------------------- constructor -----------------------------------
Employee::Employee(string last, string first, int id, int sal) {
idNumber = (id >= 0 && id <= MAXID? id : -1);
salary = (sal >= 0 ? sal : -1);
lastName = last;
firstName = first;
}
//-------------------------- destructor ------------------------------------
// Needed so that memory for strings is properly deallocated
Employee::~Employee() { }
//---------------------- copy constructor -----------------------------------
Employee::Employee(const Employee& E) {
lastName = E.lastName;
firstName = E.firstName;
idNumber = E.idNumber;
salary = E.salary;
}
//-------------------------- operator= ---------------------------------------
Employee& Employee::operator=(const Employee& E) {
if (&E != this) {
idNumber = E.idNumber;
salary = E.salary;
lastName = E.lastName;
firstName = E.firstName;
}
return *this;
}
//----------------------------- setData ------------------------------------
// set data from file
bool Employee::setData(ifstream& inFile) {
inFile >> lastName >> firstName >> idNumber >> salary;
return idNumber >= 0 && idNumber <= MAXID && salary >= 0;
}
//------------------------------- < ----------------------------------------
// < defined by value of name
bool Employee::operator<(const Employee& E) const {
return lastName < E.lastName ||
(lastName == E.lastName && firstName < E.firstName);
}
//------------------------------- <= ----------------------------------------
// < defined by value of inamedNumber
bool Employee::operator<=(const Employee& E) const {
return *this < E || *this == E;
}
//------------------------------- > ----------------------------------------
// > defined by value of name
bool Employee::operator>(const Employee& E) const {
return lastName > E.lastName ||
(lastName == E.lastName && firstName > E.firstName);
}
//------------------------------- >= ----------------------------------------
// < defined by value of name
bool Employee::operator>=(const Employee& E) const {
return *this > E || *this == E;
}
//----------------- operator == (equality) ----------------
// if name of calling and passed object are equal,
// return true, otherwise false
//
bool Employee::operator==(const Employee& E) const {
return lastName == E.lastName && firstName == E.firstName;
}
//----------------- operator != (inequality) ----------------
// return opposite value of operator==
bool Employee::operator!=(const Employee& E) const {
return !(*this == E);
}
//------------------------------- << ---------------------------------------
// display Employee object
ostream& operator<<(ostream& output, const Employee& E) {
output << setw(4) << E.idNumber << setw(7) << E.salary << " "
<< E.lastName << " " << E.firstName << endl;
return output;
}
我将包括对NULL指针的检查,但我只想让它工作,并将在包含我正在检查的数据的列表上测试它。
感谢任何可以提供帮助的人,像往常一样,这是一门课程,所以我没想到或想要答案,但任何可能出错的提示都会有很大的帮助!
答案 0 :(得分:2)
在未编译的行中,您将Node
结构(在左侧)与类型为T
的对象(在右侧)进行比较。相反,您希望比较两个类型为T
的对象。所以你需要这样做(假设,正如你所说,类型T
指定了operator==()
方法):
if (*dummyPtr->data == target) {