考虑一个std::map<const char *, MyClass*>
.
如何访问MyClass
映射指向的对象的成员(变量或函数)?
// 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
编辑——根据 Xeo 的建议
我发布了虚拟代码。这是真正的代码。
// 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 );
// .....
}
错误是:
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 –