2013-04-04 96 views
0

我:错误,当我从一个链表中删除一个节点

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. 

为什么呢?

THIS IS MY REAL CODE:

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; 
} 
+1

你能告诉我们你分配'vec'的代码吗? – 2013-04-04 02:32:09

+2

可以有很多原因。您将需要向我们展示更多代码! – Nbr44 2013-04-04 02:37:20

+0

好吧,我正在为真正的代码更新我的主题。谢谢。 – 2013-04-04 02:39:08

回答

1

所以要根据你的回应你的问题是,你正在做一个double delete这是undefined behavior。你应该从Time_LinkedList::delete_TimeNode中删除delete data,让析构函数完成它的工作。