如何在构造函数中初始化数组?

时间:2022-07-05 21:28:50

I want to be able to pass an array with a bound less than max size to a constructor and initialize private data member. The problem is I either get error: invalid initializer for array member 'option Program::long_options [10]' or error: no matching function for call to 'Program::Program(const char [8], option [4], const char [5])'. The only option is to pad out the array I'm passing to the constructor with useless entries.

我希望能够将一个小于最大大小的数组传递给构造函数并初始化私有数据成员。问题是我得到错误:数组成员'选项Program :: long_options [10]'的初始化程序无效或错误:没有匹配函数来调用'Program :: Program(const char [8],option [4],const char [5])'。唯一的选择是填充我传递给无用条目的构造函数的数组。

class Program
{
public:
    Program(const char* pn, const option (&lo)[MAX_OPTS], const char* os);
private:
    option long_options[MAX_OPTS];
};

Program::Program(const char* pn, const option (&lo)[MAX_OPTS], const char* os)
    : program_name(pn), long_options(lo), opt_string(os)
{
}

option ex_lo[] = {
    { "help",    no_argument,       nullptr,               'h' },
    { "verbose", no_argument,       &helpdata.verbose_flag, 1  },
    { "output",  required_argument, nullptr,               'o' },
    LONG_OPTION_ZEROS
};
Program example("example", ex_lo, "hvo:");

I have also tried using a vector but run into the same problem:

我也试过使用矢量但遇到同样的问题:

    std::vector<option> long_options;
};

Program::Program(const char* pn, option (&lo)[MAX_OPTS], const char* os)
    : program_name(pn), long_options{std::begin(lo), std::end(lo)}, opt_string(os)
{
}

3 个解决方案

#1


1  

The language doesn't allow you to initialize an array using another array.

该语言不允许您使用其他数组初始化数组。

int arr1[] = {10, 20};
int arr2[] = arr1 // Not allowed. In this context, arr1 evaluates to `int*`.

This prevents array member variables of classes from being initialized in the initializer list from another array.

这可以防止类的数组成员变量在初始化列表中从另一个数组初始化。

The only way to fill up the contents of the array member variables is to set the values of its elements one by one inside the body of the constructor.

填充数组成员变量内容的唯一方法是在构造函数体内逐个设置其元素的值。

You can use std::vector or std::array for member variables to make your class easier to implement.

您可以将std :: vector或std :: array用于成员变量,以使您的类更容易实现。

#2


0  

ex_lo and long_options are two distinct arrays. You can't make one into the other.

ex_lo和long_options是两个不同的数组。你不能让一个进入另一个。

What you can do:

你可以做什么:

  • Copy lo into long_options. -or-
  • 将lo复制到long_options中。 -要么-
  • Make long_options a pointer (or, I think, a reference) to an array. This allows you to keep the assignment in the constructor as it is.
  • 使long_options成为数组的指针(或者,我认为是一个引用)。这允许您按原样将赋值保留在构造函数中。

#3


0  

Maybe instead of

也许不是

option long_options[MAX_OPTS];

use

使用

option *long_options;

in this case time of life option ex_lo[] and object of Program class must be equals.

在这种情况下,生命时间选项ex_lo []和Program类的对象必须等于。

P.S. sorry for my English, i'm learning:)

附:抱歉我的英语,我正在学习:)

#1


1  

The language doesn't allow you to initialize an array using another array.

该语言不允许您使用其他数组初始化数组。

int arr1[] = {10, 20};
int arr2[] = arr1 // Not allowed. In this context, arr1 evaluates to `int*`.

This prevents array member variables of classes from being initialized in the initializer list from another array.

这可以防止类的数组成员变量在初始化列表中从另一个数组初始化。

The only way to fill up the contents of the array member variables is to set the values of its elements one by one inside the body of the constructor.

填充数组成员变量内容的唯一方法是在构造函数体内逐个设置其元素的值。

You can use std::vector or std::array for member variables to make your class easier to implement.

您可以将std :: vector或std :: array用于成员变量,以使您的类更容易实现。

#2


0  

ex_lo and long_options are two distinct arrays. You can't make one into the other.

ex_lo和long_options是两个不同的数组。你不能让一个进入另一个。

What you can do:

你可以做什么:

  • Copy lo into long_options. -or-
  • 将lo复制到long_options中。 -要么-
  • Make long_options a pointer (or, I think, a reference) to an array. This allows you to keep the assignment in the constructor as it is.
  • 使long_options成为数组的指针(或者,我认为是一个引用)。这允许您按原样将赋值保留在构造函数中。

#3


0  

Maybe instead of

也许不是

option long_options[MAX_OPTS];

use

使用

option *long_options;

in this case time of life option ex_lo[] and object of Program class must be equals.

在这种情况下,生命时间选项ex_lo []和Program类的对象必须等于。

P.S. sorry for my English, i'm learning:)

附:抱歉我的英语,我正在学习:)