Search code examples
c++vectorvisual-studio-2017

how to input structural type vector data into structural vector member (nested structural vector) in c++


I'm beginner and implementing scheduling simulation. I want to insert the structural vector(Order) into the structural member vector(selectedParts) in the Pallet.

I know how to insert the int type vector (fixtureTypes) into the structure Pallet But i don't know how to insert the structural type vector (selectedParts) into the Pallet. I'd appreciate it if someone let me know how to solve it.

int main()
{
    std::vector<Pallet> pallets;
    int tmpFixtureType;
    for (int i = 0; i < 3; ++i)
    {
        Pallet pallet;
        pallet.palletNo = i;
        for (int j = 0; j < 3; ++j)
        {
            cin >> tmpFixtureType;
            pallet.fixtureTypes.push_back(tmpFixtureType);
        }
        pallets.push_back(pallet);  //end the "pallet.fixtureTypes.push_back" loop

   }

   for (int i = 0; i < 3; ++i)     //and then input the "selectedParts" in pallets
   {
       Order tmpOrder;
       tmpOrder.partNo = j;
       tmpOrder.partType = j;
       pallet.selectedParts.push_back(tmpOrder);
   }
   pallets.push_back(pallet);

}

Solution

  • The error you are getting is that pallet as a name does not have meaning outside the scope ({ to }) where it is defined. You have multiple Pallet instances, you need to think about which one you are dealing with.

    Moving your Order population to the scope where pallet is defined is simple.

    int main()
    {
        std::vector<Pallet> pallets;
        for (int i = 0; i < 3; ++i)
        {
            Pallet pallet;
            pallet.palletNo = i;
            for (int j = 0; j < 3; ++j)
            {
                int tmpFixtureType;
                cin >> tmpFixtureType;
                pallet.fixtureTypes.push_back(tmpFixtureType);
            }
    
            for (int i = 0; i < 3; ++i)
            {
                Order tmpOrder;
                tmpOrder.partNo = j;
                tmpOrder.partType = j;
                pallet.selectedParts.push_back(tmpOrder);
            }
            pallets.push_back(pallet);
        }
    }