使用工厂方法时的编译器错误:无法转换 'const std::pair<char* const
Compiler Error When Using Factory Method: cannot convert 'const std::pair<char* const
我正在尝试为一个软件编写代码,该软件会为模拟生成不同类型的 "Cars"。我想让用户能够选择对象的数量 "cars" 进行模拟,我认为工厂方法将是最好的方法。
这是我遇到问题的代码部分:
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 我得到:
错误:无法在初始化中将‘const std::pair’转换为‘Factory*’|
at //error 2,3, & 4 我得到:
错误:从“Car*”到“Private/Police/Ambulance*”的无效转换 [-fpermissive]
这是我的工厂:
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();}
};
工厂没有错误。
Ambulance 和 Private 派生自 Car。
police来源于private.
任何帮助将不胜感激。
汽车,私人,警察类:
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...;
};
等等。
对于第一个错误,*它将 return 迭代器中正确位置的对。
所以你得到的是你应该做的对 (*it).second.
对于所有其他错误,汽车、救护车和警察应该从汽车上驾驶,它不必直接,例如,如果私人从汽车上驾驶,而不是警察从汽车上驾驶,那还好(你做得很好),但你的问题是你在不使用 static_cast 或 c cast 的情况下进行向下转换。
Police* police = static_cast<Police*>(factory->create())
并且您的代码不适用于其他类型,您需要像对待警察一样获取每种类型。
正如 thia post 中所述:Can't downcast because class is not polymorphic?.
我正在尝试为一个软件编写代码,该软件会为模拟生成不同类型的 "Cars"。我想让用户能够选择对象的数量 "cars" 进行模拟,我认为工厂方法将是最好的方法。
这是我遇到问题的代码部分:
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 我得到: 错误:无法在初始化中将‘const std::pair’转换为‘Factory*’|
at //error 2,3, & 4 我得到: 错误:从“Car*”到“Private/Police/Ambulance*”的无效转换 [-fpermissive]
这是我的工厂:
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();}
};
工厂没有错误。
Ambulance 和 Private 派生自 Car。 police来源于private.
任何帮助将不胜感激。
汽车,私人,警察类:
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...;
};
等等。
对于第一个错误,*它将 return 迭代器中正确位置的对。
所以你得到的是你应该做的对 (*it).second.
对于所有其他错误,汽车、救护车和警察应该从汽车上驾驶,它不必直接,例如,如果私人从汽车上驾驶,而不是警察从汽车上驾驶,那还好(你做得很好),但你的问题是你在不使用 static_cast 或 c cast 的情况下进行向下转换。
Police* police = static_cast<Police*>(factory->create())
并且您的代码不适用于其他类型,您需要像对待警察一样获取每种类型。
正如 thia post 中所述:Can't downcast because class is not polymorphic?.