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,
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};
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.
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.
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.
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With