刚刚想写个接口
class XXX;
class YYY
{
typedef std::map<string, XXX*> XXXList;
XXXList mXXXNodes;
XXX* getXXXNode(const std::string zzz) const
{
。。。
if (NULL == mXXXNodes[zzz];
。。。
}
};
报错提示:error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const SceneManager::SceneNodeMap' (or there is no acceptable conversion)
1> d:\program files\microsoft visual studio 8\vc\include\map(166): could be 'SceneNode *&std::map<_Kty,_Ty>::operator [](const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Kty=std::string,
1> _Ty=SceneNode *,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> while trying to match the argument list '(const SceneManager::SceneNodeMap, const std::string)'
也就是说,std::map没有重载const 参数的operator[]运算符,自己肯定不会去重载std::map的operator操作,也不愿意将接口改变为
XXX* getXXXNode(const std::string zzz)
修改的方法:其实可以用
SceneNodeMap::const_iterator i = mSceneNodes.find(pName);
if (i == mSceneNodes.end())
// 找不到该名字的节点
return NULL;
return i->second;