3

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?

Eric
  • 559
  • 2
  • 8
  • 14

3 Answers3

7

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.

Community
  • 1
  • 1
2

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.

bshields
  • 3,563
  • 16
  • 16
1

One option is to use the array class (your implementation may support std::array or std::tr1::array; if not, you can use boost::array from the Boost libraries):

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

The elements stored in the container still cannot be const; you can make the entire vector const if that works for your use case.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I'll throw in here that `blitz::Array` also works pretty well for me, though it doesn't really do read-only. – kenm Jun 17 '10 at 14:11