I wrote this function in C++ as part of a bigger program:
Object Single_list<Object>::pop_front() {
//Single_node<Object> *tmp_front;
//Object hold;
if (empty()) {
throw underflow();
}
Single_node<Object> first_node = front();
Single_node<Object> *ptr = list_head;
list_head = list_head->next();
delete ptr;
return first_node.retrieve();
}
However, when I try and use this function I get the following message:
WARNING: calling delete twice on the same memory location: 0x100100160
I am really confused, I am not deleting the pointer (assuming that’s what is causing the problem?) twice.
Any advice will be appreciated.
As requested, here is the constructor and retrieve function for the Single_Node, although I am not sure how helpful this will be to the error regarding deletion that I am getting:
template <typename Object>
Single_node<Object>::Single_node( const Object &e, Single_node<Object> *n ):element( e ), next_node( n )
{
// empty constructor
}
template <typename Object>
Object Single_node<Object>::retrieve() const
{
return element;
}
template <typename Object>
Single_node<Object> *Single_node<Object>::next() const
{
return next_node;
}
Building on Mankarse’s comment, try the following:
This grabs the result from the first node before it gets deleted.