【发布时间】:2012-12-04 05:13:15
【问题描述】:
我的代码最初看起来像这样:
int SomeObject::operator[]( const string& key ) const
{
return ( *m_Map )[ key ];
}
int SomeObject::operator()( const string& key ) const
{
return ( *m_Map2 )[ key ];
}
这两张地图都有以下签名:
std::map< std::string, int >
然后我读到了一些关于 STL 容器确实不需要显式堆分配(即std::map< ... >* map = new std::map< ... >)的内容,这就是我正在做的事情。
我将映射更改为堆栈分配并删除指针取消引用,使其看起来像这样:
int SomeObject::operator[]( const string& key ) const
{
return m_Map[ key ];
}
int SomeObject::operator()( const string& key ) const
{
return m_Map2[ key ];
}
编译器抱怨以下错误(对于两个地图):
Error 1 error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
笏。
【问题讨论】:
-
你能确切地说明
m_Map和m_Map2是如何声明的吗?
标签: visual-c++ stl hashmap