panos
panos

Reputation: 17

c++ struct of vector of struct

Im trying to get a better understanding of structs and I could use some help. Im trying to create a struct which contains a vector of structs, but Im having trouble linking them together. So far I got:

struct Vehicle{
    string LicensePlate;
    string VehicleType;
    vector<string> DamageType;
    int EstFixDays;
}veh;

struct DailyCustomers{
    string date;
    struct Vehicle vehicleList;
}dlc;

int main(){
  vector<Vehicle> vehicleList;
  struct DailyCustomers dlc;
  dlc.date = fileVector[1];
  for (int i = 1; i < fileVector.size(); i++){
      stringDevide(fileVector[i]);
      struct Vehicle veh;
      veh.LicensePlate = licensePlate;
      veh.VehicleType = vehicleType;
      veh.DamageType = vehicleDamageType;  
      veh.EstFixDays = est; 
      vehicleList.push_back(veh);
  }
  dlc.vehicleList = veh;
}

My program seems to be working fine, as the struct linking doesnt affect it, but Im not sure if I linked them together correctly. Should the last line be inside the for loop?

Any help greatly appreciated. Thank you.

Upvotes: 0

Views: 136

Answers (1)

john
john

Reputation: 87959

You haven't created a struct containing a vector of structs. All you've done is created a struct which contains a struct. You do have a vector of structs but that is in main, not in any struct.

You also have miscellaneous other issues, global variables you aren't using and unnecessary struct uses.

Here's how you should do it

struct Vehicle {
    string LicensePlate;
    string VehicleType;
    vector<string> DamageType;
    int EstFixDays;
};

struct DailyCustomers {
    string date;
    vector<Vehicle> vehicleList; // a vector of structs inside a struct
};

int main() {
    DailyCustomers dlc;
    dlc.date = fileVector[1];
    for (int i = 1; i < fileVector.size(); i++) {
        stringDevide(fileVector[i]);
        Vehicle veh;
        veh.LicensePlate = licensePlate;
        veh.VehicleType = vehicleType;
        veh.DamageType = vehicleDamageType;  
        veh.EstFixDays = est; 
        dlc.vehicleList.push_back(veh);
    }
}

Upvotes: 2

Related Questions