c++ - 重载==递归比较两个链表

标签 c++ recursion linked-list overloading friend

我有一道作业题,要求我重载 == 运算符来比较两个链表。我需要递归执行此操作。

这是我的 .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/

相关文章:

c++ - if/else 中的流提取

C++ 破坏函数错误!

c++ - 如何通过在 C++ 中创建对象来将参数传递给类?

python - 参数拆包浪费堆栈帧

java - 理解这个递归函数

c - 链表程序崩溃

c++ - std::forward_list 是否支持手动重新指向?

C++(看似)随机编译器错误

python - 使用递归按字典顺序生成排列

java - 在链表中添加节点时陷入无限循环