how to assign an array from an initializer list

前端 未结 3 1450
予麋鹿
予麋鹿 2020-12-30 04:41

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

<
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 05:27

    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
     }
    

提交回复
热议问题