简体   繁体   English

C ++中的虚方法的麻烦

[英]trouble with virtual methods in c++

guys! 伙计们! I am trying to do some tests using assertions and I wanted to create an InMemoryRepository but I receive an error. 我正在尝试使用断言进行一些测试,我想创建一个InMemoryRepository,但是收到错误消息。 This is what I want to do: 这就是我想做的:

void TestCaseMedicineInMemoryRepository::setUp(){
    this->medRepo = new MedicineInMemoryRepository(); //error
}

error: 错误:

Multiple markers at this line
    - The type 'MedicineInMemoryRepository' must implement the inherited pure virtual method 'MedicineInMemoryRepository::Save'
    - The type 'MedicineInMemoryRepository' must implement the inherited pure virtual method 'MedicineInMemoryRepository::getAll'
    - The type 'MedicineInMemoryRepository' must implement the inherited pure virtual method 
     'MedicineInMemoryRepository::findMedicineById'
    - The type 'MedicineInMemoryRepository' must implement the inherited pure virtual method 
     'MedicineInMemoryRepository::deleteMedicine'
    - The type 'MedicineInMemoryRepository' must implement the inherited pure virtual method 
     'MedicineInMemoryRepository::updateMedicine'

virtual medicineRepository: 虚拟药库

class MedicineRepository{
public:
    virtual void Save(Medicine* m) throw (RepositoryException) = 0;
    virtual void deleteMedicine(int ID) throw (RepositoryException) = 0;
    virtual void updateMedicine(Medicine* m) throw (RepositoryException) = 0;
    virtual List<Medicine*> getAll()=0;
    virtual Medicine* findMedicineById(int ID)=0;
};

InMemoryRepository.h: InMemoryRepository.h:

#include "medicineRepository.h"

class MedicineInMemoryRepository : public MedicineRepository{
protected:
    List<Medicine*> medList;
public:
    MedicineInMemoryRepository();
    virtual ~MedicineInMemoryRepository();
    void Save(Medicine* m) throw (RepositoryException) = 0;
    void deleteMedicine(int ID) throw (RepositoryException) = 0;
    void updateMedicine(Medicine* m) throw (RepositoryException) = 0;
    //int containsName(string name)=0;
    List<Medicine*> getAll()=0;
    //int updateQuantity(Medicine* m) throw (RepositoryException) = 0;
    Medicine* findMedicineById(int ID)=0;
};

InMemoryRepository.cpp: InMemoryRepository.cpp:

#include "InMemoryRepository.h"

MedicineInMemoryRepository::MedicineInMemoryRepository(){
}


MedicineInMemoryRepository::~MedicineInMemoryRepository(){
    for(int i=0; i < medList.getLen(); i++){
        delete medList.getElement(i);
    }
}

Medicine* MedicineInMemoryRepository::findMedicineById(int ID){
    for(int i=0; i < this->medList.getLen(); i++){
        Medicine* m = medList.getElement(i);
        if(m->getID()==ID){
            return m;
        }
    }
    return NULL;
}

List<Medicine*> MedicineInMemoryRepository::getAll(){
    return medList;
}

void MedicineInMemoryRepository::Save(Medicine* m) throw (RepositoryException){
    Medicine* med = findMedicineById(m->getID());
    if(med != NULL){
        medList.addElement(medList.getLen(), new Medicine(*m));
    }
    else{
        throw RepositoryException("There is a medicine with the given ID!");
    }
}

void MedicineInMemoryRepository::deleteMedicine(int ID) throw (RepositoryException){
    int i=0;
    while( i < medList.getLen() && medList.getElement(i)->getID()!=ID){
        i++;
    }
    if(i < medList.getLen()){
        Medicine* m = medList.deleteElementAtPos(i);
        delete m;
    }
    else{
        throw RepositoryException("There is no medicine with the given ID");
    }
}

void MedicineInMemoryRepository::updateMedicine(Medicine* m) throw (RepositoryException){
    Medicine* med = findMedicineById(m->getID());
    if (med != NULL){
        m->setQuantity(med->getQuantity());
        m->setName(med->getName());
    //          this->medList->getElementAtPosition(pos).setConcentration(m.getConcentration());
        deleteMedicine(med->getID());
        Save(m);
    }
}

What am I doing wrong that I receive the above error? 收到上述错误,我在做什么错? I thought about 3 hours and I don't get it!!! 我花了大约3个小时才明白!!!

   void Save(Medicine* m) throw (RepositoryException) = 0;
    void deleteMedicine(int ID) throw (RepositoryException) = 0;
    void updateMedicine(Medicine* m) throw (RepositoryException) = 0;

Remove =0; 删除=0;
While it is allowed to have definition for pure virtual functions, they still remain pure virtual and don't allow non virtual calls. 虽然可以定义纯虚函数,但它们仍保持纯虚函数,并且不允许非虚调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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