0

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;
      }
    }
  }
};
5
  • I'm not sure what your question is, but you do want to notice that you are advancing i even for an invalid entry. This is a bug. You also aren't showing the initialization of pVehicle, which if omitted or wrong could be another bug. Commented Jun 20, 2022 at 3:26
  • TLDR: use data structures from STL when possible. Here, use a std::list and append vehicles to its end with push_back. Commented Jun 20, 2022 at 3:45
  • pVehicle[i] has type Vehicle, but new Car has type Vehicle *. Of course there don't exist an operator= that assign Vehicle * to Vehicle. Commented Jun 20, 2022 at 4:03
  • Thanks for support, I use list and solved the problem Commented Jun 20, 2022 at 4:03
  • First problem you never allocate the storage for the pVehicle array. Second problem a Vehicle array can not hold Car or Truck, only a Vehicle* array can hold, in which case you should be using std::uniqe_ptr<Vehicle> or std::shared_ptr<Vehicle>. And you should be using std::vector or std::array instead of a C-style array. Don't write C code disguised as C++. Commented Jun 20, 2022 at 4:18

3 Answers 3

2

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps you meant to do:

std::array<Vehicle *, 50> pVehicle;

Which would ensure that pVehicle[i] was a pointer.

1 Comment

Maybe I use wrong syntax or whatever. I mean just want to create 1 array of Vehicle then call input function to input any type of vehicle (be it Car or Truck)
0

The problem that I see more important it's that you are trying to increment the memory address of pVehicle, even when you didn't know exactly the size of that object. in order to fix this problem, the best you can do is use one of the standard.

If you already know how much elements will store, and also you know the number of elements will not increase, I suggest you to use the std:array, otherwise if you will increasing commonly adding elements I recommend you use the std::list (this topic it's really dense, if you need to know more look https://www.geeksforgeeks.org/linked-list-vs-array/?ref=leftbar-rightbar).

enum vehicleOpt {car, track/*,.....*/}

class Vehicle {
  pure virtual void foo();
};
class Car : public Vehicle {
   void foo() override;
};
class Truck : public Vehicle {
  void foo() override;
};

void main(){
  std::list<vehicle*> vehicles;
  //std::array<vehicle*, n> vehicles;
  //....

  switch(option){
    case enumOpt::car:
      auto vehicle = new Car();
      break;
    case enumOpt::track:
      auto vehicle = new track();
      break;
      //..... as many case you need. also throw an error if not exist.
   }

   vehicle.foo();
   vehicles.push_back(vehicle);


}

In this approach I also try to use the polymorphism of the vehicle object, and try to reduce the boilerplate code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.