向量保存只读矩阵?
我想使用一个向量来保存大小为 5x5 的只读整数矩阵,
vector<const int[5][5]> startingPieces;
但是这个声明会导致一堆我以前从未见过的奇怪错误。
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> ]
那么,这个声明有什么问题呢?
I want to use a vector to hold read-only integer-matrices of the size 5x5
vector<const int[5][5]> startingPieces;
But this declaration causes a bunch of weird errors I've never ever seen before.
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> ]
So, what is wrong with this declaration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两件事 - 首先向量不能容纳 const 对象 - 请参阅 我可以在向量中使用 const 来允许添加元素,但不是对已添加的修改? 对此进行讨论。其次,它们不能保存数组,因为它们保存的内容必须是可复制和可分配的,而数组两者都不是。
Two things - firstly vectors cannot hold const objects - see Can I use const in vectors to allow adding elements, but not modifications to the already added? for a discussion of this. And secondly they cannot hold arrays, as the things they hold must be copyable and assignable, and arrays are neither.
您在这里应该做的是创建自己的矩阵类来存储 5x5 数据数组,然后用它创建向量。
What you should do here is create your own matrix class that stores the 5x5 array of data and then create your vector with that.
一种选择是使用
array
class (您的实现可能支持std::array
或std::tr1::array
;如果不支持,您可以使用boost::array
来自 Boost 库):存储在容器中的元素仍然不能是 const;如果适合您的用例,您可以使整个向量为常量。
One option is to use the
array
class (your implementation may supportstd::array
orstd::tr1::array
; if not, you can useboost::array
from the Boost libraries):The elements stored in the container still cannot be const; you can make the entire vector const if that works for your use case.