This is my code for input list vehicle (can Car or Truck). But it's not work and error: no match for 'operator=' (operand types are 'Vehicle' and 'Car*'). How I can input vehicle (can Car or Truck) for true?
class Vehicle {};
class Car : public Vehicle {};
class Truck : public Vehicle {};
class ListVehicle {
Vehicle *pVehicle;
int n;
public:
void input() {
for (int i = 0; i < n; i++) {
int type;
cout << "Enter vehicle type (1: car, 2: truck): ";
cin >> type;
cin.ignore();
switch (type) {
case 1: {
pVehicle[i] = new Car();
break;
}
case 2: {
pVehicle[i] = new Truck();
break;
}
default:
cout << "Invalid type" << endl;
break;
}
}
}
};
std::listand append vehicles to its end withpush_back.pVehicle[i]has typeVehicle, butnew Carhas typeVehicle *. Of course there don't exist anoperator=that assignVehicle *toVehicle.pVehiclearray. Second problem aVehiclearray can not holdCarorTruck, only aVehicle*array can hold, in which case you should be usingstd::uniqe_ptr<Vehicle>orstd::shared_ptr<Vehicle>. And you should be usingstd::vectororstd::arrayinstead of a C-style array. Don't write C code disguised as C++.