簡體   English   中英

對象類型之間的轉換

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM