vendredi 10 juin 2016

Initialization of array based on vector size in structure [c++]


I'm trying to create a program that split a region into subsets (8 here), and each subset contains 8 values (corresponding to predefined vectors). I defined a structure to store these values (as well as vectors):

struct Storage {
  static const int num_spatial_subset = 8;
  static const std::vector< std::vector<double> > vectors = 
    { {0,0,0}, 
      {0,1,0}, 
      {0,0,1}, 
      {1,1,0}, 
      {1,0,1}, 
      {0,1,1}, 
      {1,0,0}, 
      {1,1,1} };
  double storage[num_spatial_subset][vectors.size()];
};

However, I'm getting a compilation error at 'vectors' intialization:

 error: field initializer is not constant
       {1,1,1} };
               ^
 error: in-class initialization of static data member ‘const std::vector<std::vector<double> > Storage::vectors’ of non-literal type
 error: non-constant in-class initialization invalid for static member ‘Storage::vectors’
 error: (an out of class initialization is required)
 error: ‘Storage::vectors’ cannot be initialized by a non-constant expression when being declared
 error: array bound is not an integer constant before ‘]’ token
   double storage[num_spatial_subset][vectors.size()];

vectors will be the same for all Storage object (which is why I defined them as static const).

I know that I could replace my storage variable with a vector and resize it in the constructor of Storage, but that will involve to resize the first vector to 8, and loop to resize all inside vectors to 8 too. As I might have to create thousands of these objects, I don't think it's the optimal way to do it.

I don't really understand why compiler complains, because vectors are defined at compile time (as well as the number of vectors).

Thank you.


Aucun commentaire:

Enregistrer un commentaire