2010-06-17 72 views
3

我想用一个向量来保存尺寸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>  ] 

那么,这个声明有什么问题呢?

回答

2

你应该在这里做的是创建你自己的矩阵类,它存储5x5的数据数组,然后创建你的向量。

+0

是的,这似乎是这个问题的最简单的解决方案。 – Eric 2010-06-17 14:11:09

1

一种选择是使用the array class(您的实现可能支持std::arraystd::tr1::array;如果没有,你可以使用boost::array从Boost库):

std::vector<std::array<std::array<int, 5> > > 

储存在容器中仍然不能常量元素;如果它适用于你的用例,你可以使整个向量为常量。

+0

我会在这里抛出'blitz :: Array'对我来说也很好,尽管它并不真正做到只读。 – kwatford 2010-06-17 14:11:43