0

伙计们!我正在尝试使用断言进行一些测试,并且我想创建一个 InMemoryRepository 但我收到一个错误。这就是我想要做的:

void TestCaseMedicineInMemoryRepository::setUp(){
    this->medRepo = new MedicineInMemoryRepository(); //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'

虚拟医学资源库:

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:

#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:

#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);
    }
}

收到上述错误,我做错了什么?我想了3个小时,我不明白!

4

1 回答 1

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

移除=0;
虽然允许定义纯虚函数,但它们仍然保持纯虚函数,并且不允许非虚函数调用。

于 2013-04-21T18:56:49.413 回答