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

  • Home
  • SEARCH
  • 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 6629801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:18:44+00:00 2026-05-25T22:18:44+00:00

I am trying to write a container class with iterator. This is my class:

  • 0

I am trying to write a container class with iterator. This is my class:

template <class T>
class C{
 private:
  T* v;
  int MAX_SIZE;
  int i;
 public:
  C (int size){
   MAX_SIZE = size;
   v = new T (MAX_SIZE);
   i = 0;
  }

 ~C(){ delete v; }

  class iterator{
   private:
    T* v;
   public:
    iterator(T* ip){ v = ip; }

    void operator++ (){ ++v; }
    void operator-- (){ --v; }
    T    operator*  () { return *v; }
    bool operator!= (const iterator & it) { return v != it.v; }
 };

 iterator begin(){ return iterator (v); }
 iterator end()  { return iterator (&v[MAX_SIZE]); }

 void push_back (T e){
   if (i == MAX_SIZE) throw MaxSizeReached();
   v[i] = e;  
   ++i;
 }

 class MaxSizeReached{};
};

template <class T>
void print(typename C<T>::iterator & start, typename C<T>::iterator & end){
 for (typename C<T>::iterator s (start), e (end); s != e; ++s){
   std::cout << *s << '\n';
 }
}

int main(){
 C<int> ic (3);
 C<float> fc (4);
 C<char> cc (3);

 ic.push_back (56);
 ic.push_back (76);
 ic.push_back (88);

 print<int>(ic.begin(), ic.end());

 return 0;
}

g++ 4.5 throws this error:

templatizedCustomIterator.c++: In function ‘int main()’:
templatizedCustomIterator.c++:71:35: error: no matching function for call to ‘print(C<int>::iterator, C<int>::iterator)

Which is incorrect – definition of print() or the call?

  • 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-25T22:18:45+00:00Added an answer on May 25, 2026 at 10:18 pm

    Have a look the function template:

    template<T>
    void print(typename C<T>::iterator & start, typename C<T>::iterator & end);
    

    And your usage:

     print(ic.begin(), ic.end());
    

    So the problem is, T cannot be deduced from the function argument. The Standard calls it non-deducible context. Here I’ve explained similar question in detail, read this:

    • C++, template argument can not be deduced

    Now the question is, how would you implement the function template? So here is one good solution:

    template <class FwdIterator>
    void print(FwdIterator start, FwdIterator end)
    {
      for ( ;  start != end;  ++start)
      {
         std::cout << *start << '\n';
      }
    }
    

    If you pass a third argument as:

    template <class FwdIterator>
    void print(FwdIterator start, FwdIterator end, std::ostream &out)
    {
      for ( ;  start != end;  ++start)
      {
         out << *start << '\n';
      }
    }
    

    then you can use this to print to file as well:

     print(ic.begin(), ic.end(), std::cout); //print to console
    
    
     std::ofstream file("file.txt")
     print(ic.begin(), ic.end(), file); //print to file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write my first iterator class / container type. Basically, I want
I am trying to write a container class which uses STL allocators. What I
I am trying to write graph class as a template class. As answered in
I'm trying to write some dimension-independent code for a template class in c++, using
I'm trying to write a composite component like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I'm trying to write a generic class which contains a generic TObjectList< T >
I'm trying to write a class which contains several std::vectors as data members, and
I am new programmer in Obj-C and cocoa . Im a trying to write
I am trying to write a quadtree sparse matrix class. In short, a quadtree_matrix<T>
I'm trying to write a program in C++ language. Class Edge indicates the connection

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.