vendredi 24 février 2017

Check and Deletion not working as expected

I have a templated class

template <class T>
class Node
{
    private:
        T data;

    public:
        Node<T> *next;
        Node(T initial);
        ~Node();
};    

and the destructors are as follows:

template <class T>
Node<T>::~Node()
{
    if (std::is_pointer<T>::value)
    {
        delete this->data;
    }
}

template <class T>
LinkedList<T>::~LinkedList() {
    Node<T> *this_nodes = this->head;
    Node<T> *next = NULL;

    while (this_nodes)
    {
        next = this_nodes->next;
        delete this_nodes;
        this_nodes = next;
    }
}

However, when I attempt to compile with g++ with C++11, with the following driver code

LinkedList<int> list;
list.insert(5);
assert(list.read() == 5);

I get the following error:

error: type ‘int’ argument given to ‘delete’, expected pointer

The Node is the interior of the LinkedList, which I left off as it's not fully necessary to help answer this question. I'm not sure why this error appears as I have a check in place to assure the data is a pointer before attempting to delete it.

Can someone enlighten me a bit further on why this won't work / an alternative method? (NOTE: Cannot use smart pointers for this assignment)

Aucun commentaire:

Enregistrer un commentaire