【问题标题】:How can I assign an array from an initializer list?如何从初始化列表中分配一个数组?
【发布时间】:2015-07-22 14:51:31
【问题描述】:

我对 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

/// 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;

它返回以下错误消息:

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,

【问题讨论】:

  • 听起来您的编译器不在 C++11 兼容模式下(或不支持)?你用的是什么编译器?
  • 嗯,它说,你必须使用 -std=c++11 标志。那你为什么不呢?
  • @MikeMB 错误本身并不是因为缺乏对c++11 的支持。即使使用-std=c++11,您仍然会收到相同的错误(虽然不是警告)。即使在 C++98 代码中你也会收到警告,因为现代 g++ 编译器 (>=4.9) 将大括号解释为 std::initializer_list(即使你不使用 -std=c++11 编译)他们认为这是一个扩展,并且默认情况下启用(请参阅警告消息)。
  • @vsoftco:你当然是对的——我应该先仔细看看代码。但如果可能的话,我仍然会推荐使用 c++11 标志。

标签: c++ arrays initializer-list


【解决方案1】:

当你说

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 程序中遇到了同样的问题。

【讨论】:

    【解决方案2】:

    数组本质上只是指针。 C++(作为一种基于符号的编程语言)对数组有自己的解释。含义:

    int* a[3];您已经声明了数组,但目前分配给每个元素的值是一些垃圾值,这些值已经存储在分配给您的数组的内存位置。

    a={1,2,3};不起作用,因为:C++ 将数组名称“a”视为指向数组中第一个元素的地址位置的指针。 'a' 基本上被 C++ 解释为 '&a[0]',它是元素 a[0]

    的地址

    因此,您有两种分配值的方法

    1. 使用数组索引(如果您不知道指针是什么,您唯一的选择)

      int a[3]; for(int i=0;i>a[i]; }

    2 将其视为指针并使用指针操作

        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)
        }
    

    注意:不能使用 ++a 指针操作,因为 ++ 会改变指针的位置,而 a+i 不会改变指针 'a' 的位置,而且无论如何使用 ++ 都会产生编译器错误。

    推荐阅读 Stephen Davis C++ 的傻瓜书。

    【讨论】:

    • 数组不是指针,它们本身就是类型,例如sizeof(int[2][2]) 等于 4 * sizeof(int),而不是 sizeof(int*)2 * sizeof(int*)。可以将数组分配给适当类型的指针,但它们是不同的类型(我认为这种分配行为是 C 和 C++ 中的缺陷)。
    【解决方案3】:

    您不能在声明后直接分配给数组。基本上你的代码是一样的

    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
     }
    

    【讨论】:

      猜你喜欢
      • 2013-03-14
      • 2013-09-21
      • 2012-11-24
      • 2020-05-09
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      相关资源
      最近更新 更多