小编Aus*_*tin的帖子

关于随机访问迭代器的运算符+重载的问题,在模板中

我想在列表类中重载迭代器的"+"运算符,类似于

list<double>::iterator operator+(const list<double>::iterator& it, int n)
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,当我尝试将其实现为模板时,就像

template<class T>
typename list<T>::iterator operator+(const typename list<T>::iterator& it, int n)
Run Code Online (Sandbox Code Playgroud)

我收到错误信息,如,

no match for 'operator+' in 'it + small_index'
Run Code Online (Sandbox Code Playgroud)

无法弄清楚原因......

代码附在下面,

#include<iostream>
#include<list>
using namespace std;

template<class T>
ostream& operator<< (ostream& os, const list<T>& l)
{
  typename list<T>::const_iterator i = l.begin();
  for (;i!=--l.end();i++)
    os<<*i<<";";
  os<<*i<<endl;
  return os;
}

template<class T> //this is where it goes WRONG.
                  //If don't use template, delete "typename", T->double, runs well
typename list<T>::iterator operator+(const typename list<T>::iterator& it, int …
Run Code Online (Sandbox Code Playgroud)

c++ templates iterator overloading operator-keyword

2
推荐指数
1
解决办法
553
查看次数

标签 统计

c++ ×1

iterator ×1

operator-keyword ×1

overloading ×1

templates ×1