【问题标题】:Unable to access iterator data member using object无法使用对象访问迭代器数据成员
【发布时间】:2019-09-16 20:52:01
【问题描述】:

我正在尝试重载 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&lt;type&gt;::Iterator iter; 作为 largeInt 类中的私有数据成员。

迭代器类保存在另一个类中,它嵌套在一个链表类中,这就是为什么要创建一个迭代器对象我必须做List&lt;type&gt;::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;
 }

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

【问题讨论】:

  • *other.iter 怎么样?

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2014-04-29
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多