我有:
class Node {
int* vec;
Node* next;
};
Class LinkedList{
Node* head;
}
我创建了一个找到我要删除的节点的函数:
Node* tmp = find("abc");
我订购指针并调试它,一切正常。
现在我必须删除tmp
,所以我尝试了:
delete[] tmp->vec;
delete tmp; // here I get an error window.
为什么?
这是我的真实代码:
class Show_Time
{
private:
string movieCode;
string movieName;
string time; //the time of screening the movie.
};
class Time_LinkedList
{
private:
class Time_Node
{
public:
Show_Time* data;
Time_Node* prev;
Time_Node* next;
public:
Time_Node(string movie_code, string movie_name, string time_of_movie); //"Time_Node" constructor
~Time_Node();//"Time_Node" destructor
};
Time_Node* head; //pointer to the first node in the linkedlist
};
void Time_LinkedList::delete_TimeNode(string movieCode, string time)
{
Time_Node* tmp = find_Time_Node(movieCode,time);
// the case that there is one element in the list
if (tmp->prev == NULL && tmp->next == NULL)
{
head = NULL;
}
// the case that it's the first element of the list
else if (tmp->prev == NULL)
{
head = tmp->next;
tmp->next->prev = head;
}
// the case that it's the last element of the list
else if (tmp->next == NULL)
{
tmp->prev->next = NULL;
}
// there are element in the left and right of the element
else
{
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
}
// delete the temp and its data
delete tmp->data;
delete tmp;
}
答案 0 :(得分:1)
因此,根据您的回答,您的问题是您正在double delete
undefined behavior。您应该从delete data
中删除Time_LinkedList::delete_TimeNode
并让析构函数完成它的工作。