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 6960253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:22:43+00:00 2026-05-27T15:22:43+00:00

I am currently trying to build an iterator for a custom class and it’s

  • 0

I am currently trying to build an iterator for a custom class and it’s not going particularly well: The problem I’m having is that I my custom class uses a template and looks something like this:

template<class T>
class MyClass {
private:
    T memberVar;
};

Now, MyClass is basically a wrapper for memberVar which at the moment is a map or a map of maps. Now, I would like to create an iterator in another class and do useful things with it, i.e. access elements in my map or map of maps.

I have tried forwarding my iterator from my map, which doesn’t give me an error in MyClass when I do something like typedef T::iterator iterator;, but it obviously doesn’t want to be nice to me when I want to call myIterator->first, because now it doesn’t believe me anymore that this will work. To be honest, I was surprised to see that my typedef actually works.

Now my question: Is there a nice way to do what I would like to do? Or have I manoeuvred myself into a corner here? Thanks very much in advance!

Simon

Some more information:

In a ClassA I instantiate a ClassB which then inherits MyClass with the right type T.
Then, in ClassA I also instantiate a ClassC which I give a reference to ClassB. Now, the error occurs, when I try to create myIterator in ClassC and try to do myIterator->first.

  • 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-27T15:22:44+00:00Added an answer on May 27, 2026 at 3:22 pm

    For starters, if your class template is always going to be a wrapper for
    some instantiation of map, I’d repeat all of the typedefs in
    std::map in your class, each time with:

    typedef typename T::value_type;
    // ...
    

    (The typename is necessary, at least according to the standard, but
    version of g++, and compile all of your code with it, even if you use a
    different compiler for the final builds.)

    With regards to the myIterator->first not working, you’ll have to tell
    me where this line occured. There should be no problem if it is in a
    function in your class template, because the function shouldn’t be
    instantiated until it is used (and by that type, the type of T is
    known). If it is outside of the class template, it should still work,
    provided you’ve declared the variable correctly, something like:

    MyClass<std::map<T1, T2> >::iterator myIterator;
    

    (Within the class template, just using iterator as the typename should
    suffice, since the typedef is in scope.)

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

Sidebar

Related Questions

I'm currently trying to upgrade our build server at work, going from having no
I am currently trying to build some test code that uses Google C++ Test
I am currently trying to build on TeamCity, which uses Microsoft.Office.Interop dll. This will
I'm currently trying to build an application that inherently needs good time synchronization across
I am trying to build an API that gives data on various cities. Currently,
I'm trying to build firefox but I'm having some problems. I currently have Visual
I'm struggling a lot trying to get our build server going. I am currently
I'm currently trying to build a class hierarchy automatically by using C++ templates. The
I'm currently trying to build a custom connection manager and custom data flow source
I am currently trying to build a cakephp app that will list items from

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.