C++ 如何从初始化列表中分配数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30178879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:43:16  来源:igfitidea点击:

how to assign an array from an initializer list

c++arraysinitializer-list

提问by Dalek

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

我对c++ 的了解有限。我试图编译一个C++库,当我运行以下头文件的 make 文件时

mcmc_dhs.h

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,

I would appreciate if someone can help.

如果有人可以提供帮助,我将不胜感激。

回答by vsoftco

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:

或使用循环(或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
 }

回答by Miles C.

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 work around, but it is somewhat tedious. All you have to do is assign the values one by one. For example:

这是不正确的,因为 C++ 不允许您重新分配数组值。有一个简单的解决方法,但它有点乏味。您所要做的就是一一分配值。例如:

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.

等等。我在 Arduino 程序中遇到了同样的问题。

**I am NOT a c++ connoisseur so this is subject to incorrectness.

**我不是 C++ 鉴赏家,所以这可能有误。