我有一道作业题,要求我重载 == 运算符来比较两个链表。我需要递归执行此操作。
这是我的 .h 文件
class LList {
public:
friend bool operator == (const LList& lfSide, const LList& rtSide);
private:
struct Node {
int item;
Node* next;
};
friend bool operator == (const LList& lfSide, Node* headlf, const LList& rtSide, Node* headrt);
Node* head;
}
我尝试使用辅助函数来实现递归,但它仍然报错说 Node is not defined。
friend bool operator == (const LList& lfSide, Node* headlf, const LList& rtSide, Node* headrt);
谁能帮我解决这个问题?
最佳答案
结构体Node是私有(private)数据成员,不能在友元声明中使用
friend bool operator == (const LList& lfSide, Node* headlf, const LList& rtSide, Node* headrt);
你可以这样定义结构
#include <iostream>
class LList {
public:
friend bool operator == (const LList& lfSide, const LList& rtSide);
private:
struct Node {
int item;
Node* next;
bool operator ==( const Node &headrt);
};
Node* head;
};
并且在友元运算符中使用结构节点的运算符==。
关于c++ - 重载==递归比较两个链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58104318/