[英]Conversions between object types
我正在嘗試做的是:
void startApp() {
//create validator
MedValidator* val = new MedValidator();
//create reporsitory
MedicineRepository* repo= new MedRepo() ;
//create controller
Control wh(repo, val);
...}
這是使用類型的視圖:
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;
};
我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);};
我該如何解決?我希望代碼量足夠多,如果需要更多代碼,我會添加。
Control
的構造函數采用MedRepo*
參數:
Control(MedRepo* repo, MedValidator* validator);};
但是您正在通過MedicineRepository*
:
MedicineRepository* repo= new MedRepo() ;
//create controller
Control wh(repo, val);
另外,不要使用異常規范,因為它們很糟糕 。
您的問題是MedRepo是MedicineRepository,但MedicineRepository不是MedRepo。 您不能在期望派生類對象的地方替代基類對象,而只能相反(安全地,無論如何)。 您需要確定是否需要指向該特定派生類的指針,或者是否可以指向任何派生類的指針。 對於前者,請保持其代碼的狀態,然后將其發送給MedRepo對象。 如果有任何派生類會做(即,您僅從基類訪問方法),則將Control改為接受基類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.