简体   繁体   中英

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

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* repo, MedValidator* validator);};

But you are passing a 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM