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
It seems that
m_componentsis of typeComponentMap*.When you write
m_components["Armor"]compiler interprets that as an access to"Armor"-th element of dynamic array ofComponentMaps, which does not make any sense.What you want is
(*m_components)["some string"]. This will invokeoperator[]ofComponentMap, 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 usefind.The simplified edition will be:
This code does not have the same behaviour as your original example and will fail if
m_componentsdoes not have"Armor","Weapon"and"Attributes"elements.The closest we can get is to explicitly handle absence of element and return
0ornullptrif you use C++11.Final correct C++03 compatible edition: