[英]Conversions between object types
What I'm trying to do: 我正在尝试做的是:
void startApp() {
//create validator
MedValidator* val = new MedValidator();
//create reporsitory
MedicineRepository* repo= new MedRepo() ;
//create controller
Control wh(repo, val);
...}
Here is a view at the used types: 这是使用类型的视图:
class MedValidator
{
public:
void validate(const Medicine& s) throw(MedException);
};
class MedicineRepository
{
public: virtual void addMed(Medicine s) ;
};
class MedRepo : public MedicineRepository{
public:void addMed(Medicine s);
protected:
Vector<Medicine*> MedList;
};
I get Multiple markers at this line - candidates are: - no matching function for call to 'Control::Control(MedicineRepository&, MedValidator*&)'
at startApp()
when I'm declaring wh
我Multiple markers at this line - candidates are: - no matching function for call to 'Control::Control(MedicineRepository&, MedValidator*&)'
得到Multiple markers at this line - candidates are: - no matching function for call to 'Control::Control(MedicineRepository&, MedValidator*&)'
当我声明wh
时,在startApp()
Multiple markers at this line - candidates are: - no matching function for call to 'Control::Control(MedicineRepository&, MedValidator*&)'
class Control {
public:
Control(MedRepo* repo, MedValidator* validator);};
How can I fix this?I hope the amount of code is enough,if it's needed more I'll add. 我该如何解决?我希望代码量足够多,如果需要更多代码,我会添加。
The constructor for Control
takes a MedRepo*
argument: Control
的构造函数采用MedRepo*
参数:
Control(MedRepo* repo, MedValidator* validator);};
But you are passing a MedicineRepository*
: 但是您正在通过MedicineRepository*
:
MedicineRepository* repo= new MedRepo() ;
//create controller
Control wh(repo, val);
Also, don't use exception specifications, they're bad . 另外,不要使用异常规范,因为它们很糟糕 。
Your problem is that MedRepo is a MedicineRepository but MedicineRepository is not a MedRepo. 您的问题是MedRepo是MedicineRepository,但MedicineRepository不是MedRepo。 You can't substitute a base class object where a derived class object is expected, only the reverse (safely, anyway). 您不能在期望派生类对象的地方替代基类对象,而只能相反(安全地,无论如何)。 You need to figure out if you need a pointer to that specific derived class or if a pointer to any derived class is okay. 您需要确定是否需要指向该特定派生类的指针,或者是否可以指向任何派生类的指针。 For the former keep your code how it is and send it a MedRepo object. 对于前者,请保持其代码的状态,然后将其发送给MedRepo对象。 If any derived class will do (ie you're only accessing methods from the base class) then change Control to accept the base class instead. 如果有任何派生类会做(即,您仅从基类访问方法),则将Control改为接受基类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.