Consider a std::map<const char *, MyClass*>.
How do I access a member (variable or function) of the MyClass object pointed to by the map?
// assume MyClass has a string var 'fred' and a method 'ethel'
std::map<const char*, MyClass*> MyMap;
MyMap[ "A" ] = new MyClass;
MyMap.find( "A" )->fred = "I'm a Mertz"; // <--- fails on compile
MyMap.find( "A" )->second->fred = "I'm a Mertz"; // <--- also fails
EDIT — per Xeo’s suggestion
I posted dummy code. Here is the real code.
// VarInfo is meta-data describing various variables, type, case, etc.
std::map<std::string,VarInfo*> g_VarMap; // this is a global
int main( void )
{
// ........ g_VarMap["systemName"] = new VarInfo;
g_VarMap.find( "systemName" ).second->setCase( VarInfo::MIXED, VarInfo::IGNORE );
// .....
}
errors were:
struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VarInfo*> >’ has no member named ‘second’
Field 'second' could not be resolved Semantic Error make: *** [src/ACT_iod.o] Error 1 C/C++ Problem
Method 'setCase' could not be resolved Semantic Error –
std::mapstores types internally as astd::pair, andstd::map::find, returns aniterator. So, to access members of your class, you have to go through theiterator, which presents thekey_typeasfirst, and thevalue_typeassecond. Also, as others have stated, you should probably not be usingconst char*as yourkey_type. Here’s a short example.