I am trying to write a code for a software that will generate different types of "Cars" for a simulation. I want to give the user the ability to choose the number of objects "cars" for the simulation and i figure factory method will be best way to do it.
Here is the part of the code that i am having problems with:
typedef map<char *,Factory *> CarFactories;
CarFactories carFactories;
carFactories["Private"] = new privateFactory();
carFactories["Police"] = new policeFactory();
carFactories["Ambulance"] = new ambulanceFactory();
cout << "Self-Driving Cars Simulatator V1.0" << endl;
cout << "Enter number of vehicles in the simulation > " <<endl;
cin >> numCars;
for(int i = 0; i <= numCars; i++){
int numType;
char vehicleType;
//char *ptrVT;
cout << "enter the number vehicle type to add > " << endl;
cout << " 1- Private Vehicle." << endl;
cout << " 2- Police Car." << endl;
cout << " 3- Ambulance." << endl;
cin >> numType;
if (numType == 1)
vehicleType = 'Private';
else if (numType == 2)
vehicleType = 'Police';
else if (numType == 3)
vehicleType = 'Ambulance';
else
cout << "Invalid entry.";
//ptrVT = &vehicleType;
CarFactories::const_iterator it=carFactories.find("Police");
if (it!=carFactories.end())
Factory *factory = *it; //error 1
Private *priv = factory->create(); //error 2
Police *pol = factory->create(); //error 3
Ambulance *amb = factory->create(); //error 4
}
at //error 1 i am getting: error: cannot convert ‘const std::pair’ to ‘Factory*’ in initialization|
at //error 2,3, & 4 i am getting: error: invalid conversion from ‘Car*’ to ‘Private/Police/Ambulance*’ [-fpermissive]
Here are my factories:
class Factory
{
public:
virtual Car *create() = 0;
};
class privateFactory : public Factory {
public:
Private *create() { return new Private();}
};
class policeFactory : public privateFactory {
public:
Police *create() { return new Police();}
};
class ambulanceFactory : public Factory {
public:
Ambulance *create() { return new Ambulance();}
};
No errors there with the factories.
Ambulance and Private are derived from Car. Police is derived from private.
Any help will be greatly appreciated.
Car, private, police classes:
class Car
{
public:
Car();
..setters and getters.
~Car();
protected:
...double, int, chat variables...
};
class Police : public Private
{
public:
Police();
..setters and getters.
~Police();
protected:
... int variables...;
};
and so on.