在类中初始化boost :: multi_array

Eag*_*gle 7 c++ boost class

首先,我想说我是新手.

我想boost:multi_array在课堂上初始化.我知道如何创建boost:multi_array:

boost::multi_array<int,1> foo ( boost::extents[1000] );
Run Code Online (Sandbox Code Playgroud)

但作为课程的一部分,我遇到了问题:

class Influx {
    public:
    Influx ( uint32_t num_elements );
    boost::multi_array<int,1> foo;

private:

};

Influx::Influx ( uint32_t num_elements ) {
    foo = boost::multi_array<int,1> ( boost::extents[ num_elements ] );
}
Run Code Online (Sandbox Code Playgroud)

我的程序通过编译,但在运行期间,当我尝试从foo(例如foo[0])控制元素时,我收到错误.

如何解决这个问题呢?

小智 8

使用初始化列表(顺便说一下,我知道关于Boost的这一点的拉链,所以我按照你的代码):

Influx::Influx ( uint32_t num_elements ) 
   : foo( boost::extents[ num_elements ] ) {
}
Run Code Online (Sandbox Code Playgroud)

  • 确保你理解为什么.此外,单击绿色复选标记接受答案.:) (4认同)