Search code examples
c++virtual

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

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

What am I doing wrong that I receive the above error? I thought about 3 hours and I don't get it!!!


Solution

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

    Remove =0;
    While it is allowed to have definition for pure virtual functions, they still remain pure virtual and don't allow non virtual calls.