C++ 如何从初始值设定项列表分配数组
我对c++的知识有限。我试图编译一个c++库,当我为下面的头文件运行make文件时 mcmc_dhs.hC++ 如何从初始值设定项列表分配数组,c++,arrays,initializer-list,C++,Arrays,Initializer List,我对c++的知识有限。我试图编译一个c++库,当我为下面的头文件运行make文件时 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 /// parame
#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;
如果有人能帮忙,我将不胜感激。您不能在数组声明后直接将其分配给数组。基本上,您的代码与
int main()
{
double arr[2][2];
arr = { {1, 2}, {3, 4.5} }; // error
}
您必须在声明时赋值
double arr[2][2] = { {1, 2}, {3, 4.5} };
或者使用循环(或std::copy
)分配元素。由于数组似乎是一个成员变量,因此也可以在构造函数初始化列表中对其进行初始化:
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
}
当你说:
boundaries =
{{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};
不正确,因为C++不允许您重新分配数组值。有一个简单的工作,但它有点乏味。你所要做的就是一个接一个地赋值。 例如:
boundaries[0][0] = 0;
boundaries[0][1] = 512;
boundaries[1][0] = 0;
boundaries[1][1] = 512;
等等。我在Arduino项目中也遇到了同样的问题
<>我不是C++鉴赏家,所以这是错误的。 数组基本上是指针。C++(作为符号的编程语言)对数组有自己的解释。意思是: int*a[3];您已经声明了数组,但当前分配给每个元素的值是一些垃圾值,这些值已存储在分配给数组的内存位置 a={1,2,3};不能工作,因为C++将数组名称“A”作为指向数组中第一元素的地址位置的指针。A基本上是由C++解释为A和A(0),这是元素A(0)< /P>的地址。 因此,您有两种方法来分配值
int a[3];
for(int i=0;i<3;++i) // using for loop to assign every element a value
{
cin>>*(a+i); // store value to whatever it points at starting at (a+0) upto (a+2)
}
inta[3];
对于(int i=0;i>*(a+i);//将值存储到从(a+0)开始到(a+2)的任何位置
}
注意:不能使用++指针操作,因为++更改了指针的位置,而a+i不会更改指针“a”的位置,并且使用++将导致编译器错误
<>推荐Stephen Davis C++为傻瓜书。< /P>听起来好像你的编译器不是C++ 11兼容模式(或者不支持它)?您使用的编译器是什么?可能重复的是,它说,您必须使用-std=c++11标志。那么为什么不使用呢?@MikeMB错误本身并不是因为缺乏对
c++11
的支持。即使使用-std=c++11
,您仍然会得到相同的错误(但不是警告)即使在C++ 98代码中也会得到警告,因为现代G++编译器(>=4.9)将括号解释为<代码> STD::IngalIZELSYList(即使你没有用<代码> -STD= C++ 11 <代码>编译),他们认为这是一个扩展,默认情况下是启用的(参见警告消息)@vsoftco:你当然是对的-我应该先仔细看一下代码。但如果可能的话,我还是建议使用c++11标志。数组不是指针,它们本身就是类型,例如sizeof(int[2][2])
等于4*sizeof(int)
,而不是sizeof(int*)
或2*sizeof(int*)
。可以将数组分配给适当类型的指针,但它们是不同的类型(我认为这种分配行为在C和C++中是一种缺陷)。
int a[3];
for(int i=0;i<3;++i) // using for loop to assign every element a value
{
cin>>*(a+i); // store value to whatever it points at starting at (a+0) upto (a+2)
}