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

The Archive Base Latest Questions

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

In my header file I have included the std::map and use the appropriate namespace.

  • 0

In my header file I have included the std::map and use the appropriate namespace.
One of my members is:

map<unsigned int, double> pT_Spam;

And in my .cpp file I attempt to do something that I have been doing frequently for some time now:

for(map<unsigned int, double>::iterator it=pT_Spam.begin() ; it!=pT_Spam.end() ; it++) {/*code*/}

The above is even mentioned in one the examples of using std::map at cplusplus.com . Even though I have done pretty much the same in other parts of the code which cause no compile errors, on this particular line I get the following error from Cygwin:

error: conversion from `std::_Rb_tree_const_iterator<std::pair<const unsigned int, double> >' to non-scalar type `std::_Rb_tree_iterator<std::pair<const unsigned int, double> >' requested

Which seems rather strange. Any idea what might be wrong? (my header is, of course, included in my .cpp)

  • 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-27T22:47:14+00:00Added an answer on May 27, 2026 at 10:47 pm

    It would seem that at the scope this loop exists, the map is const. For example, is the loop in a class method declared const, like so?

    void method() const // const method
    {
        // Do stuff.
    }
    

    or passed as a const argument, like this?

    void function(const map<unsigned int, double>& pT_Spam)
    {
        // Do stuff.
    }
    

    If it is, you must use const iterators:

    for(map<unsigned int, double>::const_iterator it=pT_Spam.begin() ; it!=pT_Spam.end() ; it++)
    {
        /*code*/
    }
    

    Or, if you’re using C++11, then you should use the auto keyword:

    for(auto it=pT_Spam.begin() ; it!=pT_Spam.end(); it++)
    {
        /*code*/
    }
    

    Since in the case you’ve shown you must use const iterators, you can’t use them to modify the map or the data within it. That’s const correctness, and it’s a good thing :).

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

Sidebar

Related Questions

I have a header file in which I wish to use a namespace alias
I have a header file. In this header I want to use a map
I have a header file, lets say Common.h, that is included in all of
I have a file called header.html and it is included by base.html . In
I have a c++ header file containing a class. I want to use this
I have the following code in a header included in main.mm: 1. virtual int
Let's say I have a header file with a class that uses std::string .
I have Visual studio 2005 . One of my header file has a enum
I have a class which defines the following members in a header file (.hpp):
I have C++ project (VS2005) which includes header file with version number in #define

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.