cancel
Showing results for 
Search instead for 
Did you mean: 

Initialization problem with custom containers in a vector

GMeur
Senior

Hi,

Consider the following two CustomContainers and the Screen class :

class CustomContainer : public touchgfx::Container
{
 
public:
 
    CustomContainer()
    {
        add(_a_text_area);
    }
    void setup(); //a function to setup the element
    void writeText(const std::u16string txt)
    {
        _a_text_area.setVisible(false);
        _a_text_area.invalidate();
        _txt_buffer = txt;
        _a_text_area.setWildcard((uint16_t*)&_txt_buffer[0]);
        _a_text_area.resizeToCurrentText();
        _a_text_area.setVisible(true);
        _a_text_area.invalidate();
    }
private:
    touchgfx::TextAreaWithOneWildcard _a_text_area;
    std::u16string _txt_buffer;
};
 
class CustomContainerBis : public touchgfx::Container
{
 
public:    
    CustomContainerBis();
    void setup(const uint8_t elements_nb)
    {
        //here, I've tried various other solutions to setup the vector 
        _custom_containers = std::vector<CustomContainer>(6, CustomContainer());
 
 
        //place CustomContainer objects and set them up        
        int y = 0;
        for (int i = 0; i < 6; i++)
        {
            _custom_containers[i].setup();
            _custom_containers[i].setPosition(0, y, 50, 15);
            add(_custom_containers[i]);
            y += 20;
        }
 
        setWidth(15);
        setHeight(y);
    }
    void fillInElements()
    {
        for (int i = 0; i < (int)_custom_containers.size(); i++)
        {
            _custom_containers[i].writeText(u"a_text");
        }
    }
 
private:
    std::vector<CustomContainer> _custom_containers;
};
 
 
class Screen : public ScreenBase
{
public:
    Screen()
    {
        _a_custom_container.setXY(20, 20);
        _a_custom_container.setup(5);
        add(_a_custom_container);
    }
 
    void aFunction()
    {
        _a_custom_container.fillInElements();
    }
private:
    CustomContainerBis _a_custom_container;
}

In this code, the line _a_text_area.invalidate() within the function writeText() throws an error message (reference to unknown object). What is it I'm doing wrong? I suspect it has to do with the call to the add(_a_text_area) fuction which is inside the default constructor…

Any help is more than welcome ! I've been struggling with this for quite some time now and tried a lot of different things… Ofc I can make it work by creating a vector of pointers to CustomContainer but It's not something I wish to do.

Guillaume.

Ps : excuse if there are any mistakes that slipped in the code above, I wrote it all off the top of my head…

0 REPLIES 0