c++, boost, store objects in multidimensional array without default constructor -


i want store thousands of interpolation functions in multidimensional array, preferable 1 boost. main problem interpolation function use class not have default constructor. prohibits me initialize multidimensional array.

what wish do:

double func(const double& x1, const double& x2, const double& x3) {   return x1 + x2 + x3; };  int main() {   std::vector<double> x1 {0, 1, 2, 3};   std::vector<double> x2 {1.1, 1.2, 1.3, 1.4, 1.5};   std::vector<double> x3 {0, 10, 20, 30, 40};   std::vector<double> y(20, std::vector<double>(5));    boost::multi_array<linear_interp, 2> storage(boost::extents[4][5]);    typedef std::vector<double>::size_type vd_sz;   int n = 0;   (vd_sz i_x1 = 0; i_x1 < x1.size(); ++i_x1) {     (vd_sz i_x2 = 0; i_x2 < x2.size(); ++i_x2) {       for( vd_sz i_x3 = 0; i_x3 < x3.size(); ++i_x3) {         y[n][i_x3] = func(x1[i_x1], x2[i_x2], x3[i_x3]);       }       linear_interp myinterp(x3, y);       storage[i_x1][i_x2] = myinterp;       ++n;     }   }    // sample usage   double z = storage[3][2].interp(23);    return 0; } 

the problem class linear_interp has no default constructor (the class similar class 1), therefore boost::multi_array can not initialize array.

note initialize interpolations inside loop , therefore, need store these objects. simple pointer object not work, since object overwritten in each loop.

in reality, have more dimensions (atm have 10), , multi_array nice container handle these. additionally, interpolations in later loops, take interpolations previous loops (i.e. have recursive problem).

edit 1: minor code correction.

edit 2: code correction: in previous version, did not save "y"s lead unwanted results.

well pointer work. if declare array as:

multi_array<linear_interp*, 2>  

to store pointers the objects instead of objects themselves. in loop allocate new object each time necessary , put appropriate place in array. use new keyword create new linear_interp object inside loop. here code use inside loop:

storage[i_x1][i_x2] = new linear_interp(x3, y); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -