0

I have a class with some private members containing objects and a dynamic array of pointers which I want to fill with pointers to some of those member object.

class NextionTest : public NextionDisplay {
private:
  NexText           lblText =   NexText(0,  1, "t0");
  NexButton       btnPage1  =   NexButton(  0,  2,  "b0");
  NexButton       btnPage0  =   NexButton(  1,  1,  "b0");

  NexTouch *nex_listen_list[] = {    
                &lblText,
                &btnPage0,  
                &btnPage1,
                nullptr 
  };
 /* rest of class not shown */
};

The above code result in this error:

too many initializers for 'NexTouch* [0]'

How to solve this?

nrussell
  • 18,382
  • 4
  • 47
  • 60
Bascy
  • 2,017
  • 1
  • 21
  • 46
  • I get this error in VS: `'NextionTest::nex_listen_list': array bound cannot be deduced from an in-class initializer` (you have to explicitly state the array size) – qxz Dec 07 '16 at 22:30

1 Answers1

0

You can initialize you array in this way:

NexTouch* nex_listen_list[4] = {
    &lblText,
    &btnPage0,
    &btnPage1,
    nullptr
};

However, this defines a fixed size, C-style array, and it seems to me (from seeing the "null-termination") that your intent is to have a dynamic array, which grows/shrinks and is always terminated by nullptr similarly to strings. In this case, it is better to use std::vector:

std::vector<NexTouch*> nex_listen_list = {
    &lblText,
    &btnPage0,
    &btnPage1,
    nullptr
};

Most likely the nullptr-termination is not needed, but it depends on what you want to do with the array. Maybe you will send it to some API that takes an old-style array? In this case you can use nex_listen_list.data() to retrieve the embedded C-style array.

A.S.H
  • 29,101
  • 5
  • 23
  • 50