首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用工厂方法时编译器错误:无法转换'const std::pair<char* const‘

使用工厂方法时编译器错误:无法转换'const std::pair<char* const‘
EN

Stack Overflow用户
提问于 2016-12-11 20:42:49
回答 1查看 118关注 0票数 0

我正试图为一个软件编写一个代码,该软件将生成不同类型的“汽车”来进行模拟。我想给用户的能力,选择对象的数量“汽车”的模拟,我认为工厂的方法将是最好的方法。

下面是代码中我遇到问题的部分:

代码语言:javascript
复制
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
        }

在//error 1,我得到: error:无法在初始化中将‘const std::结对’转换为‘Factory*’

在//error 2,3 &4我得到:错误:从“汽车*”到“私有/警察/救护车”的无效转换( -fpermissive )

这里是我的工厂:

代码语言:javascript
复制
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();}
    };

工厂里没有错误。

救护车和私人汽车是从汽车衍生而来的。警察是从私底下来的。

任何帮助都将不胜感激。

私家车、私人、警察班:

代码语言:javascript
复制
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...;
};

诸若此类。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-11 21:29:57

对于第一个错误,*它将返回迭代器中的corrent poaition中的对。所以你得到的是一对你应该做的(*it).second。

对于所有其他的错误,汽车,胆量和警察应该被驱赶出汽车,它不需要被淋湿,例如,如果私人驾驶汽车和警察开车超过它的ok(你做得很好),但你的问题是,你是做下演员没有使用static_cast或c演员。

代码语言:javascript
复制
Police* police = static_cast<Police*>(factory->create())

您的代码将不适用于其他类型,您将需要得到每一种类型,就像你对警察一样。

如在thia后的教导:Can't downcast because class is not polymorphic?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41090948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档