无法使用对象访问迭代器数据成员

时间:2019-04-27 17:01:34

标签: c++ c++11 data-structures linked-list operator-overloading

我正在尝试重载operator +,我想使用迭代器添加两个节点,但是在从另一个对象访问迭代器时遇到问题。

这是我的接线员+:

 type operator+(const largeInt<type> &other) {
     iter = list.end();
     other.iter = other.list.end() //need help here

     type newNumb1, newNumb2;

     newNumb1 = *iter;
     newNumb2 = other.*iter; //need help here

     return newNumb1 + newNumb2;
 }

我有这个typename List<type>::Iterator iter;作为largeInt类中的私有数据成员。

迭代器类保存在另一个类中,它嵌套在链表类中,这就是为什么我必须做一个迭代器对象List<type>::Iterator的原因,尽管它可以工作,但是我无法使用另一个largeInt对象访问它作为参考。

更新:

 type operator+(const largeInt<type> &other) {
     typename List<type>::Iterator other_iter = other.iter; 
     type newNumb1, newNumb2;

     newNumb1 = *iter;
     newNumb2 = *other_iter;

     return newNumb1 + newNumb2;
 }

这行得通,但是我想做同样的事情,但是不必做额外的迭代器,任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

在这个简单的示例中,您根本不需要使用任何局部变量:

type operator+(const largeInt<type> &other) {
    return *iter + *(other.iter);
}