virtual destructor?

573 views Asked by At

getting a couple errors that it's a virtual function with non-virtual destructor. how can i fix this error? it was working in g++ 4.6.x but i need it to work on an older version of g++. :(

#include <list>

template <typename T>
class container{
public:
    ~queue(){
        queuel.clear();
        queuel.~list();
    }
    virtual T func_x(){
        T temp;
        //do stuff with list<t> test
        return temp;
    }
private:
    std::list<T> test;
};

template <typename T>
class container2 : public container<T>{
public:
    virtual T func_x(){
        T temp;
        //do different stuff with list<T> test
        return temp;
    }
};
1

There are 1 answers

3
Joachim Isaksson On BEST ANSWER

Edit: @Als has a point, the destructor is also incorrectly named for your class. Updated answer.

The problem is just that g++ thinks that since you have virtual methods in the class, you should have a virtual destructor. This has to do with that if you inherit a class and extend it, you may allocate memory or other resources that the base class destructor has no idea about cleaning up, and if your destructor is not virtual, the base class' destructor may be called in some cases instead of your real destructor.

The rule of thumb is, if you have virtual methods in your class, use a virtual destructor. Removing the incorrectly named destructor ~queue and putting

virtual ~container();

in your class declaration and impementing the destructor in the .cpp file should fix your problem.