4

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.

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)
{
}
1
  • Use std::vector or std::array Commented Sep 25, 2014 at 17:02

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

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

Comments

0

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

What you can do:

  • Copy lo into long_options. -or-
  • 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.

Comments

0

Here is a sample to initialize array of objects in constructor:

class B
{
public:
    B(int a, int b, int c)
    {
        std::cout << "Contructing B with " << a << b << c;
    }
};

class C
{
public:
    C(char a, bool b, std::string c)
    {
        std::cout << "Contructing C with " << a << b << c;
    }
};

class A
{
    B   m_b[2][2];
    C   m_c;
    A(int a, char b, bool c, std::string d):
    m_b{
        {
            {a,b,30},
            {a,b,30}
        },
        {
            {a,b,30},
            {a,b,30}
        }
    },
    m_c{b,c,d}
    {
        std::cout << "Contructing A with " << a << b << c << d << std::endl;
    }
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.