0

I try to initialize structures of vectors that contains vector:

    struct productionCanvas
    {
        int canvasID;
        int indexXmlJob;
    };
    
    struct productionArea
    {
        int areaID;
        std::vector<productionCanvas> canvasList;
    };

The first level is correctly initialized but not the second one:

    int areaIncr=0;
    int canvasIncr=0;
    std::vector<productionArea> production;
    
    int addArea()
    {
        productionArea area{};
    
        areaIncr++;
        area.areaID = areaIncr;
        area.canvasList = {};
        production.push_back(area);
        return areaIncr;
    }
    
    int addCanvas(int job)
    {
        productionCanvas canvas{};
    
        canvasIncr++;
        canvas.canvasID = canvasIncr;
        canvas.indexXmlJob = job;
        for (productionArea area : production)
        {
            if (area.areaID == areaIncr)
            {
                area.canvasList.push_back(canvas);  // this line is triggered
                break;
            }
        }
        // Check if correctly push_back
        for (productionArea area : production)
        {
            for (productionCanvas canvas : area.canvasList)
            {
                // This line is never triggered, why ?
            }
        }
        return canvasIncr;
    }
    
    addArea();  // area correctly inserted
    addCanvas(1);  // canvas not inserted inside area
    .

There is no return value of the push_back function, so I cannot know why it's not pushed back! It seemed that it does not work like Qt-QVector.

How to initialize these struct?

Chrigou
  • 71
  • 9

2 Answers2

1

You can zero-initialize any POD struct or array by simply adding {}:

productionArea area{};
// Or
productionArea area = {};
// Or
auto area = productionArea{};

Unless you are working in C/C++ environment, there is no need for typedef struct in C++. The symbol namespaces are not separate. This is equal to your current code:

struct productionCanvas_def
{
    int canvasID;
    int indexXmlJob;
} productionCanvas;

You can even initialize each member:

struct foo{
    int a{2};
    int a{4};
};

But then it is no longer POD.

After the question has been edited

The problem is here:

for (productionArea area : production) // <-- The `area` is a copy of 
                                       //     the one in the container.
        {
            if (area.areaID == areaIncr)
            {
                area.canvasList.push_back(canvas);  // this line is triggered
                break;
            }
        }

To modify elements in place, you have to use references for(productionArea &area : production)

Quimby
  • 17,735
  • 4
  • 35
  • 55
  • Thanks @Quimby I did as you says but I'm still not able to initialize the area.canvasList by push_back(canvas) (at the line: // this line is triggered ) – Chrigou Mar 26 '21 at 15:25
  • @Chrigou Oh, I see where you problem is, your `for` loops modify the copies. You need to use `(productionArea &area : production)` to change the `area` inside the `production` container. – Quimby Mar 26 '21 at 15:30
  • @Chrigou See https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const , the recap in the accepted answer. – Quimby Mar 26 '21 at 15:30
  • Thank you very much @Quimby, finally it works very well now! – Chrigou Mar 26 '21 at 15:42
  • @Chrigou Glad to be of help :) – Quimby Mar 26 '21 at 15:52
0

it doesn't look like c++ , but the below code wiil not run as your canvasList with empty in addArea

area.canvasList = {};
User
  • 572
  • 2
  • 10
  • Seems to be the problem, but I don't understand where is the error, it compile and run without crash. Is there a solution how to initialize this structure? – Chrigou Mar 26 '21 at 09:38