Eri*_*ric 3 c++ const vector matrix
我想使用向量来保存大小为5x5的只读整数矩阵
vector<const int[5][5]> startingPieces;
Run Code Online (Sandbox Code Playgroud)
但是这个声明引起了一系列我以前从未见过的奇怪错误.
error C2535: 'const int (*std::allocator<_Ty>::address(const int (&)[5][5]) const)[5][5]' : member function already defined or declared
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\xmemory(109) : see declaration of 'std::allocator<_Ty>::address'
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\vector(429) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\vector(439) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
1> with
1> [
1> _Ty=const int [5][5],
1> _Alloc=std::allocator<const int [5][5]>
1> ]
1> c:\users\eric\documents\visual studio 2008\projects\testing grounds\testing grounds\main.cpp(14) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=const int [5][5]
1> ]
Run Code Online (Sandbox Code Playgroud)
那么,这个宣言有什么问题呢?
小智 7
两件事 - 首先矢量不能容纳const对象 - 请参阅我可以在向量中使用const来允许添加元素,但不能修改已添加的元素吗?对此进行讨论.其次,它们不能保存数组,因为它们所拥有的东西必须是可复制和可分配的,而数组也不是.