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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:28:28+00:00 2026-06-15T11:28:28+00:00

My code originally looked something like this: int SomeObject::operator[]( const string& key ) const

  • 0

My code originally looked something like this:

int SomeObject::operator[]( const string& key ) const
{
    return ( *m_Map )[ key ];
}

int SomeObject::operator()( const string& key ) const
{
    return ( *m_Map2 )[ key ];
}

Both of these maps had the following signature:

std::map< std::string, int >

And then I read something about STL containers really having no need for explicit heap allocation ( i.e. std::map< ... >* map = new std::map< ... > ), which is what I was doing.

As soon as I change the maps to being stack allocated and remove the pointer dereferences so that it looks like this:

int SomeObject::operator[]( const string& key ) const
{
    return m_Map[ key ];
}

int SomeObject::operator()( const string& key ) const
{
    return m_Map2[ key ];
}

The compiler complains with the following error ( for both maps ):

Error   1   error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)

Wat.

  • 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-06-15T11:28:29+00:00Added an answer on June 15, 2026 at 11:28 am

    The problem is that you’ve marked the functions as const and operator[]() on std::map modifies the map (if the key is not in the map, an element is added with a default value).

    You could get away with this when using the pointer because the const only applied to the member pointer, not to the object the pointer referred to.

    Something like the following should get around the const problem:

    int SomeObject::operator[]( const string& key ) const
    {
        std::map<string,int>::const_iterator it(m_Map.find(key));
    
        if (it == m_Map.end()) {
            throw /* something */;
        }
    
        return it->second;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A co-worker asked about some code like this that originally had templates in it.
I'm having difficulty testing controllers. Original my controller for testing looked something like this:
I have just refactored a colleague's code that, roughly, looked like this... public class
This code comes from a tutorial so it's not originally my own work. What
Note: This is not a problem i'm experiencing, but it is something i'd like
I'm wondering if the following may be possible using something like jQuery. I've looked
In Python 2.5 I stored data using this code: def GLWriter(file_name, string): import cPickle
So let's say I have a code checkout that I used --ignore-externals on originally.
here is the original code: public static int getInt () { Scanner in =
I'm having trouble converting the following VB6 code into c#4.0? This function takes a

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.