delete abstract non virtual dtor c++

  abstract-class, abstract-data-type, c++, pointers

Hi everyone hope you’re all fine.

I have an interface that represent Abstract Data Type ‘Bag’. To implement that abstract data type i used Array based and Linked based implementation.

Here the definition of class

template<class ItemType>
class BagInterface{

    virtual int getCurrentSize() const = 0;
    virtual bool isEmpty() const = 0;
    virtual bool add(const ItemType& newEntry) = 0;
    virtual bool remove(const ItemType& anEntry) = 0;
    virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
    virtual bool contains (const ItemType& anEntry) const = 0;

    virtual void clear() = 0;
    virtual vector<ItemType> toVector() const = 0;

And my 2 derived class implement these methods on their way.

My link based implementation is using virtual destructor because unlike array based implementation it’s dynamically allocating memory and eventually it has to delete instance by using ‘delete’ keyword due to avoid from memory leaks.

here is the code:

template<class ItemType>
    clear(); // Clears bag's content.

After all implementation issues, i wanted to test my ADT operations.

I’ve created a function that takes a pointer as a input to represent bag.

void bagTester(BagInterface<int>* bagPtr){
   // do some test }

in my main function, after i finished my test i wanted to delete my bagPtr because i’m finished with it so it’s time to delete it.

  int main(){
     BagInterface<int>* bagPtr = nullptr;
     char userChoice;
     cin>> userChoice;
     if(userChoice == 'A'){
     bagPtr = new ArrayBag<int>(); // Array based implementation 
    }else if(userChoice == 'L'){
     bagPtr = new LinkedBag<int>(); // Link bases implementation

    bagTester(bagPtr); // test my bag
    delete bagPtr; // and now i'm finished with test let's delete the pointer
    bagPtr = nullptr; // deallocate

**In that point my error is occurring, compiler gives a warning -> **

enter image description here

I checked this related topic -> Suppress delete-non-virtual-dtor warning when using a protected non-virtual destructor

And i tried one of the suggestion was indicated in there, which is:

Guideline #4: A base class destructor should be either public and virtual, or protected and nonvirtual.

And also tried to add virtual destructor to BagInterface, but it didn’t work.

I don’t understand the problem because both of derived class have their own destructor, arrayBag has default destructor, linkedBag has virtual destructor.

So, how can i avoid from warning?

Source: Windows Questions C++