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

The Archive Base Latest Questions

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

This comes from a larger context, however I have stripped alot of code to

  • 0

This comes from a larger context, however I have stripped alot of code to simplify the question. If you feel I have left anything out please let me know.

Lets say you have a template class defined as:

#include <map>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
template <class key, class container, class container_type>
class file_to_map
{
public:
  file_to_map()
  {
    m_file = "";
  }

  file_to_map(std::string file)
  {
    m_file = file;
  }

  ~file_to_map()
  {
  }

  std::map<key, container>& get_map()
  {
    return m_map;
  }

  void set_file(const std::string file)
  { 
    m_file = file;
  }

  void insert_into_map(key insert, container_type value)
  {
    m_map[insert].insert(value);
  }

  friend std::ostream& operator<< (std::ostream &out, file_to_map<key, container, container_type> &obj)
  {
    typedef typename std::map<key, container>::const_iterator mapItr;
    mapItr mbi = obj.m_map.begin();
    mapItr emi = obj.m_map.end();
    while (mbi != emi) {
      out << " -- " << mbi->first << " -- " << std::endl;
      container::iterator cbi;
      ++mbi;
    }
    return out;
  }

  friend std::istream& operator>> (std::ifstream &in, file_to_map<key, container, container_type> &obj)
  {
    if (in.is_open())
      in.close();

    if (obj.m_file == "")
      return in;

    in.open(obj.m_file.c_str(), std::ios::in);

    if (in.fail() || in.bad()) {
      in.close();
      return in;
    }

    std::vector<key> tmp;
    typedef std::istream_iterator<key> string_input;
    copy(string_input(in), string_input(), back_inserter(tmp));
    typename std::vector<key>::iterator bvi = tmp.begin();
    typename std::vector<key>::iterator evi = tmp.end();
    while (bvi != evi) {
      obj.m_map[*(bvi)] = container();
      ++bvi;
    }

    in.close();
    return in;
  }  
  private:
    std::map<key, container> m_map;
    std::string m_file;
};

and inside the friend method “operator<<“

You want to print the conents of the map and the generic container how would one go about doing this? How do you get an appropriate iterator to iterate through the contents of the generic container.

I am trying it with:

container::iterator cbi;

CODE UPDATE

With:

friend std::ostream& operator<< (std::ostream &out, file_to_map<key, container, container_type> &obj)
  {
    typedef typename std::map<key, container>::const_iterator mapItr;
    mapItr mbi = obj.m_map.begin();
    mapItr emi = obj.m_map.end();
    while (mbi != emi) {
      out << " -- " << mbi->first << " -- " << std::endl;
      typename container::const_iterator cbi = mbi->second.begin();
      typename container::const_iterator ebi = mbi->second.end();
      std::copy(cbi, mbi, std::ostream_iterator<container_type>(out, "\t\n"));
      ++mbi;
    }
    return out;
  }

I am getting the following compiler error:

g++ -o file_to_map -Wall ./file_to_map.h ./main.cpp ./code_finder.cpp \
        -L/usr/local/boost_1_48_0/stage/lib -lboost_filesystem -lboost_system -I /usr/local/boost_1_48_0/
In file included from ./code_finder.h:4,
                 from ./code_finder.cpp:1:
./file_to_map.h: In function ‘std::ostream& operator<<(std::ostream&, file_to_map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)’:
./code_finder.cpp:36:   instantiated from here
./file_to_map.h:62: error: no matching function for call to ‘copy(std::_Rb_tree_const_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, operator<<(std::ostream&, file_to_map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)::mapItr&, std::ostream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char> >)’

What am I missing?

  • 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-27T12:13:22+00:00Added an answer on May 27, 2026 at 12:13 pm

    Use typename:

    typename container::iterator cbi;
    

    Because iterator is a dependent name, as it depends on the type argument container of the class template. Interestingly, if you’ve corrected used typename in other place, such as in operator<<:

    typedef typename std::map<key, container>::const_iterator mapItr;
    

    Since you’re working with const_iterator for the map which means the container objects which you would get using map’s const iterator will be const objects, which in turn implies you need to use const_iterator for the containers as well, because they are the values of the map. So I think you need to use this:

    typename container::const_iterator cbi; //use this instead!
    

    Reply to your edit:

    I see a typo here:

    typename container::const_iterator cbi = mbi->second.begin();
    typename container::const_iterator ebi = mbi->second.end();
    std::copy(cbi, mbi, std::ostream_iterator<container_type>(out, "\t\n"));
                // ^^^ typo
    

    The second argument to std::copy should be ebi, not mbi. That is why I usually name such variables as begin and end, instead of cbi and ebi.

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

Sidebar

Related Questions

I have a language-agnostic question about an algorithm. This comes from a (probably simple)
This question comes from my experience with the following question: https://stackoverflow.com/questions/492748/new-responses-icon-on-so-crashes-ie7-closed In that question,
I'm trying to find out where this function comes from. Any one have any
I am trying to understand this inline assembly code which comes from _hypercall0 here
This regex comes from Atwood and is used to filter out anchor tags with
What does the PCGPRLEN-1..28 means here?? Where does this 4 bit comes from? alt
I have this function to edit all fields that come from the form and
This question has been puzzling me for a long time now. I come from
This may be a bit of daft question, but I don't come from an
We have received Java code from a software supplier. It contains a lot of

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.