Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:53:59+00:00 2026-05-23T23:53:59+00:00

I would like to overload the + operator of iterator in list class, something

  • 0

I would like to overload the “+” operator of iterator in list class, something like

list<double>::iterator operator+(const list<double>::iterator& it, int n)

This works well. However, when I try to implement it as template, like

template<class T>
typename list<T>::iterator operator+(const typename list<T>::iterator& it, int n)

I got error msg like,

no match for 'operator+' in 'it + small_index'

can’t figure out the reason…

The code is attached below,

#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 n)
{
  typename list<double>::iterator temp=it;
  for(int i=0; i<n; i++)
    temp++;
  return temp;
}

template <class T>
void small_sort(list<T>& l)
{
  int n = l.size();
  typename list<T>::iterator it = l.begin();
  for(int i=0; i<n-1; i++)
    {
      //Find index of next smallest value
      int small_index = i;
      for(int j=i+1; j<n; j++)
    {
      if(*(it+j)<*(it+small_index)) small_index=j;
    }
      //Swap next smallest into place
      double temp = *(it+i);
      *(it+i) = *(it+small_index);
      *(it+small_index)=temp;
    }
}

int main()
{
  list<double> l;
  l.push_back(6);
  l.push_back(1);
  l.push_back(3);
  l.push_back(2);
  l.push_back(4);
  l.push_back(5);
  l.push_back(0);

  cout<<"=============sort the list=============="<<endl;
  small_sort(l);
  cout<<l;
  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:54:00+00:00Added an answer on May 23, 2026 at 11:54 pm

    The problem is that the argument is not deducible in that context.

    template<class T>
    typename list<T>::iterator operator+(const typename list<T>::iterator& it, int n);
    

    When you use an iterator there, the compiler would have to generate all possible instantiations of list with any given type, and try to match an internal type iterator with the argument that you are passing, note that the set of all types is actually infinite once you add templates to the mix, as you can instantiate a template with an instantiation of the same template ad infinitum.

    I recommend that you avoid the problem altogether and use std::advance which is the idiomatic way of advancing an iterator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to overload the (/) operator in F# for strings and preserve
Specifically, I would like to create an Array class and would like to overload
I'm trying to overload the assignment operator and would like to clear a few
I would like to overload << operator for chaining like below function1(param1, param2)<< obj2
I'm using Excel VBA to a write a UDF. I would like to overload
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I have a class that contains decoded video frames. I would like my decoder
Instead of having an add() method in the repository, I would like to overload
I have a class which contains a few boost::numeric::ublas::matrix's within it. I would like
Just wondering if this is possible. What I would actually like to do is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.