2010-06-17 73 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