c ++中的虚方法问题

时间:2013-04-21 18:54:54

标签: c++ virtual

家伙!我试图使用断言进行一些测试,我想创建一个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'

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:

#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个小时,我不明白!!!

1 个答案:

答案 0 :(得分:4)

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

删除=0;
虽然允许对纯虚函数进行定义,但它们仍然是纯虚拟的,不允许非虚拟调用。