2

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,
                NULL 
  ];
 /* rest of class not shown */
};

The above code result in this error:

capture of non-variable 'NextionTest::lblText' &lblText,

I tried to move the initialization of the nex_listen_list to a init method but this gives the same result. I have no idea what a capture is.. but seems i'm doing something wrong. How to solve this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Bascy
  • 2,017
  • 1
  • 21
  • 46
  • 2
    my innocent guess (not really familiar with c++11 and beyond) is that `[]` looks like you wanted to declare a lambda. e.g. `[x]` as part of a lambda means: capture x by value. Try `{}` instead, or `{{}}` or whatever is correct ;) I am really no expert on initializer list – 463035818_is_not_an_ai Dec 07 '16 at 21:55
  • 1
    Your compiler things you are declaring a lambda function. What is the definition of NexTouch? And if it was, say an array of pointers of, toy should use aggregate initialization: http://en.cppreference.com/w/cpp/language/aggregate_initialization – Jonas Dec 07 '16 at 21:57
  • 4
    Instead of guessing about syntax I suggest picking up a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Captain Obvlious Dec 07 '16 at 22:00
  • @CaptainObvlious Thanks for your very constructive comment – Bascy Dec 07 '16 at 22:02
  • `nex_listen_list` is not a dynamic array, you probably want a `std::vector`. The way this is set up though, if your object is moved or copied you will be left with a bunch of references, either dangling or into another object. – Ryan Haining Dec 07 '16 at 22:15
  • Bascy, refrain from moving the bar on an answered question by changing the question to match the answer's suggestion. Instead ask a new question. – user4581301 Dec 07 '16 at 22:15

2 Answers2

0

I'd suggest using a std::vector, which is dynamic and knows its size.

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

  std::vector<NexTouch*> nex_listen_list = {    
                &lblText,
                &btnPage0,  
                &btnPage1};
 /* rest of class not shown */
};

However this class will behave poorly if copied or moved. If I did

NextionTest n1;
auto n2 = n1;

n2.nex_listen_list will have pointers into the data members of n1, so you should delete the copy and move operations

class NextionTest : public NextionDisplay {
    NextionTest(const NextionTest&) = delete;
    NextionTest& operator=(const NextionTest&) = delete
    //...
};
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
0

You can initialize your array this way:

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

However, this defines a fixed size array, and it seems from seeing the "null-termination" that your intent is to have a dynamic array that 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
};

The nullptr-termination is usually not needed, but depends on what you want to do with the array. If you will send it to some API that takes an old-style array you can use nex_listen_list.data() to retrieve the embedded C-style array.

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