0

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.

  • You want to use `std::string` as the key of your map and `it->second` instead of `*it` to access the factory. Even though you may get your code to compile by using a `char const*` instead of a `char*` this wouldn't do what you want it to do (it would conpare pointers, not the strings). – Dietmar Kühl Dec 11 '16 at 20:46

1 Answers1

0

for the first error, *it will return the pair in the corrent poaition in the iterator. so what you get is pair you should do (*it).second.
for all the other errors car,embolance and police should be drived from car it doesnt have to be driectly, for example if private drived from car and than police drived from it than its ok(you did it fine) but your problem is that you are doing down cast without using static_cast or c cast.

Police* police = static_cast<Police*>(factory->create())

and your code wont work for the other types you will need to get each type as you did with police.

as mentined in thia post : Can't downcast because class is not polymorphic?.

Community
  • 1
  • 1