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;
}
}
}
};
CodePudding user response:
Perhaps you meant to do:
std::array<Vehicle *, 50> pVehicle;
Which would ensure that pVehicle[i]
was a pointer.
CodePudding user response:
Here is my workaround
list<Vehicle *> vehicles;
case 1:
{
Car *car = new Car();
car->input();
vehicles.push_back(car);
break;
}
case 2:
{
Truck *truck = new Truck();
truck->input();
vehicles.push_back(truck);
break;
}