Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assign an array from an initializer list?

I have a limited knowledge about c++. I tried to compile a c++ library and when I run the make file for the following header file

mcmc_dhs.h

#include <algorithm>
#include <map>

// intrinsic shape and (reduced) shear just add?
//#define WLNOISE

// use shear instead of reduced shear for model
//#define NOREDSHEAR

/// parameters for the M200-concentration relation
const number mcreal[2] = {9.59,-0.102}; // Dolag et al. (2004)
//const number mcreal[2] = {5.26,-0.100}; // Neto et al. (2007) [Millenium Run]

/// critical density at z=0 (h100=1) in [Msun/Mpc^3]
const number rhocrit = exp(log(rhoCrit)+3.*log(Mpc)-log(Msun)); 

/// two extra halo parameters: r200 (and concentration: 2)
#define PARAMS 1

/// define region (square; twice value here) about halo that considers sources for model
#define REGION 10.0*arcmin

class mcmc_dhs : public mcmc
{
 public:

  mcmc_dhs() : 
  data(), cosmohandler(0.3,0.7,0.21,0.8,0.04),
    lenseff(), intrvar()
    {
      boundaries = 
    {{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};
    }
  ~mcmc_dhs() {}

  /// size of grid for looking up sources
  static const int Ngrid = 200;

It returns the following error message:

mcmc_dhs.h:55:67: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
      boundaries = {{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};
                                                                   ^
mcmc_dhs.h:55:17: error: assigning to an array from an initializer list
      boundaries = {{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};
                 ^
In file included from ../modules/matrix.h:15:0,
                 from ../modules/probdensity.h:4,
                 from ../modules/mcmc.h:4,
                 from mcmc_dhs.h:4,
like image 486
Dalek Avatar asked May 11 '15 22:05

Dalek


People also ask

How do you assign an array in C++?

Here is what you can do: #include <algorithm> int array [] = {1,3,34,5,6}; int newarr [] = {34,2,4,5,6}; std::copy(newarr, newarr + 5, array); However, in C++0x, you can do this: std::vector<int> array = {1,3,34,5,6}; array = {34,2,4,5,6};

How do you initialize an entire array with any value in CPP?

Using Initializer List. int arr[] = { 1, 1, 1, 1, 1 }; The array will be initialized to 0 if we provide the empty initializer list or just specify 0 in the initializer list.

How do you initialize an entire array with the same value?

Initializer List: To initialize an array in C with the same value, the naive way is to provide an initializer list. We use this with small arrays. int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index.

How can we initialize an array?

The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.


2 Answers

You cannot assign directly to an array after its declaration. Basically your code is the same as

int main()
{
    double arr[2][2];
    arr = { {1, 2}, {3, 4.5} }; // error
}

You have to either assign the value at declaration

double arr[2][2] = { {1, 2}, {3, 4.5} };

or use a loop (or std::copy) to assign elements. Since your array seems to be a member variable, you can also initialize it in the constructor initialization list:

 mcmc_dhs() : data(), cosmohandler(0.3,0.7,0.21,0.8,0.04), 
              lenseff(), intrvar(), 
              boundaries{{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}}
 { 
    // rest of ctor implementation
 }
like image 152
vsoftco Avatar answered Oct 08 '22 20:10

vsoftco


When you said

boundaries = 
{{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};

it was incorrect, because C++ does not let you reassign array values. There is an easy workaround, but it is somewhat tedious. All you have to do is assign the values one by one.

For example:

boundaries[0][0] = 0;
boundaries[0][1] = 512;
boundaries[1][0] = 0;
boundaries[1][1] = 512;

and so on. I had this same problem in an Arduino program.

like image 23
Miles C. Avatar answered Oct 08 '22 20:10

Miles C.