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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:36:44+00:00 2026-06-16T17:36:44+00:00

I am trying to access elements of a map data structure by key, but

  • 0

I am trying to access elements of a map data structure by key, but am getting a compiler error. I have defined my map data structure using typedefs to simplify the syntax for map instantiation. As you can see, the key is of type string and the data are custom GameComponent objects:

typedef map<string, GameComponent*> ComponentMap;
typedef map<string, GameComponent*>::iterator ComponentMapIter;
typedef map<string, GameComponent*>::const_iterator ComponentMapCIter;

In a derived class of GameComponent, I am creating standard Composite pattern methods along with accessors for each unique GameComponent object stored in my map. However, using the array subscript operator to access objects in accessors results in the compiler error:

void Character::add(const string& key, GameComponent* comp)
{
    m_components->insert( make_pair(key, comp) );
}

void Character::remove(const string& key)
{
    m_components->erase(key);
}

Armor* Character::getArmor() const
{
    // ERROR:
    return static_cast<Armor*>(m_components["Armor"]);
}

Weapon* Character::getWeapon() const
{
    // ERROR:
    return static_cast<Weapon*>(m_components["Weapon"]);
}

Attributes* Character::getAttributes() const
{
    // ERROR:
    return static_cast<Attributes*>(m_components["Attributes"]);
}

The output of the compiler error shows an “invalid type” error, which has me scratching my head:

/Users/Dylan/Desktop/RPG/character.cpp: In member function 'Armor* Character::getArmor() const':
/Users/Dylan/Desktop/RPG/character.cpp:66: error: invalid types 'ComponentMap* const[const char [6]]' for array subscript
/Users/Dylan/Desktop/RPG/character.cpp: In member function 'Weapon* Character::getWeapon() const':
/Users/Dylan/Desktop/RPG/character.cpp:71: error: invalid types 'ComponentMap* const[const char [7]]' for array subscript
/Users/Dylan/Desktop/RPG/character.cpp: In member function 'Attributes* Character::getAttributes() const':
/Users/Dylan/Desktop/RPG/character.cpp:76: error: invalid types 'ComponentMap* const[const char [11]]' for array subscript
  • 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-16T17:36:45+00:00Added an answer on June 16, 2026 at 5:36 pm

    It seems that m_components is of type ComponentMap*.
    When you write m_components["Armor"] compiler interprets that as an access to "Armor"-th element of dynamic array of ComponentMaps, which does not make any sense.

    What you want is (*m_components)["some string"]. This will invoke operator[] of ComponentMap, but as Luchian Grigore and Olaf Dietsche mention, std::map::operator[] does not have a const overload, so this will fail too. The only option left is to use find.

    The simplified edition will be:

    Armor* Character::getArmor() const
    {
        return static_cast<Armor*>(m_components->find("Armor")->second);
    }
    
    Weapon* Character::getWeapon() const
    {
        return static_cast<Weapon*>(m_components->find("Weapon")->second);
    }
    
    Attributes* Character::getAttributes() const
    {
        return static_cast<Attributes*>(m_components->find("Attributes")->second);
    }
    

    This code does not have the same behaviour as your original example and will fail if m_components does not have "Armor", "Weapon" and "Attributes" elements.
    The closest we can get is to explicitly handle absence of element and return 0 or nullptr if you use C++11.

    Final correct C++03 compatible edition:

    Armor* Character::getArmor() const
    {
        ComponentMapCIter i = m_components->find("Armor");
        if (i != m_components->end())
            return static_cast<Armor*>(i->second);
        return 0;
    }
    
    Weapon* Character::getWeapon() const
    {
        ComponentMapCIter i = m_components->find("Weapon");
        if (i != m_components->end())
            return static_cast<Weapon*>(i->second);
        return 0;
    }
    
    Attributes* Character::getAttributes() const
    {
        ComponentMapCIter i = m_components->find("Attributes");
        if (i != m_components->end())
            return static_cast<Attributes*>(i->second);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some xml data and I am trying to access some elements. The
I have a window with the following elements, and I'm trying to access the
I'm trying to access the width of certain elements by using jQuery's .width() function,
I'm trying to access a data attribute ('maxselection') of several html elements while attaching
I am trying to access elements of a structure from ctypes. The structure is
I have an ASP.NET site that I am trying to access div elements by
I'm trying to access elements using jquery $('#elementID') method. If any element contain .
I am trying to initialize a STL map using C++11 syntax but that doesn't
i am trying to access an element by using an ID of one of
I am trying to access all the data from database listOfFolder table folder and

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.