为什么我不能引用包含对象引用的 std::vector?以下代码在 Visual C++ 2010 中生成编译错误:
void Map::Filter( Object::Type type, std::vector<Object&>& list )
{
for ( register int y = 0; y < MAP_H; y++ )
for ( register int x = 0; x < MAP_W; x++ ) {
if ( under[y][x].GetType() == type )
list.push_back(under[y][x]);
if ( above[y][x].GetType() == type )
list.push_back(above[y][x]);
}
}
总结编译错误:
c:\program files\microsoft visual studio 10.0\vc\include\xmemory(137): error C2528: 'pointer' : pointer to reference is illegal
c:\program files\microsoft visual studio 10.0\vc\include\vector(421) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
我通过从“对象引用向量”切换到“对象指针向量”解决了这个问题。我只是不明白为什么我不能引用引用向量。