2017-06-10 33 views
1

這裏是我正在處理的LinkedList實現。它工作正常的任何數據類型,但是當我試圖做出對與Visual Studio我得到指定RtlValidateHeap 無效地址調試鏈表 一種類型的鏈表(00790000,007B16D0)自定義鏈接列表創建RtlValidateHeap錯誤與具有鏈接列表的結構

問題出現代碼如下:

typedef unsigned long int LENGTH_T; 
template < typename type > 
struct nodes 
{ 
    type _value; 
    nodes<type> * _next_node; 
    nodes() { _next_node = nullptr; } 
}; 

template < typename type > 
class LinkedList 
{ 
    nodes<type> * _elem_nodes; 
    LENGTH_T _size; 
public: 
    nodes<type> * node_at(LENGTH_T at); 
    type& operator[] (LENGTH_T at); 
    void push_back(const type src); 
    LENGTH_T size() const { return _size; } 
    LinkedList(); 
    ~LinkedList(); 
}; 

template<typename type> 
nodes<type>* LinkedList<type>::node_at(LENGTH_T at) { 
    if (at == 0) 
     return _elem_nodes; 
    else if (at > _size - 1 || _size == 0) { 
     PRINT_ERROR("try to access out of range"); 
    } 

    // tmp node for storing sequential nodes 
    nodes<type> * cur_tmp_node_ptr = _elem_nodes->_next_node; 

    for (size_t i = 1; i < at; i++) 
     cur_tmp_node_ptr = cur_tmp_node_ptr->_next_node; 

    return cur_tmp_node_ptr; 
} 

template<typename type> 
type & LinkedList<type>::operator[](LENGTH_T at) 
{ 
    return node_at(at)->_value; 
} 

template<typename type> 
void LinkedList<type>::push_back(const type src) 
{ 
    if (_size == 0) { 
     _elem_nodes->_value = src; 
     _size++; 
    } 
    else { 
     nodes<type> * new_node = new nodes<type> ; 
     new_node->_value = src; 
     new_node->_next_node = nullptr; 
     node_at(_size - 1)->_next_node = new_node; 
     _size++; 
    } 
} 

template<typename type> 
LinkedList<type>::LinkedList() 
{ 
    _size = 0; 
    _elem_nodes = new nodes<type>; 
    _elem_nodes->_value = type(); 
    _elem_nodes->_next_node = nullptr; 
} 

template<typename type> 
LinkedList<type>::~LinkedList() 
{ 
    if (_size > 1) // When size = 0 , so _size-1 = -1 but _size is unsigned; 
     for (LENGTH_T i = _size - 1; i > 0; i--) { 
      delete (node_at(i)); 
     } 
    delete (_elem_nodes); 
} 

這裏是在規定的問題可以看出 FE

struct test { 
    int anything; 
}; 

struct test2 { 
    LinkedList<test> t; 
}; 

int main() 
{ 
    LinkedList<test2> t; 
    t.push_back(test2()); 
    t.push_back(test2()); 
    return 0; 
} 

的代碼示例**編輯:我寫了自定義Assig市民明白運營商和拷貝構造函數和沒有得到這個錯誤了,但在測試2上面的例子()。t._next_node總是包含垃圾值而不是nullptr這我不理解爲什麼**

template<typename type> 
LinkedList<type>& LinkedList<type>::operator=(const LinkedList<type>& other) 
{ 
    if (&other == this) 
     return *this; 
    this->~LinkedList(); 
    this->_elem_nodes = nullptr; 
    _size = 0; 
    nodes<type> * cur_this_node = this->_elem_nodes; 
    nodes<type> * cur_other_node = other._elem_nodes; 
    while (cur_other_node != nullptr) 
    { 
     cur_this_node = new nodes<type>; 
     cur_this_node->_value = cur_other_node->_value; 
     this->_size++; 
     cur_this_node = cur_this_node->_next_node; 
     cur_other_node = cur_other_node->_next_node; 
    } 
    return *this; 
} 

template<typename type> 
LinkedList<type>::LinkedList(const LinkedList<type>& other) 
{ 
    _size = 0; 
    nodes<type> * cur_this_node = this->_elem_nodes; 
    cur_this_node = nullptr; 
    nodes<type> * cur_other_node = other._elem_nodes; 
    while (cur_other_node != nullptr) 
    { 
     cur_this_node = new nodes<type>; 
     cur_this_node->_value = cur_other_node->_value; 
     cur_this_node->_next_node = nullptr; 
     this->_size++; 
     cur_this_node = cur_this_node->_next_node; 
     cur_other_node = cur_other_node->_next_node; 
    } 
} 

回答

0

您有一個規則三次(或四次或五次)違規。你已經定義了一個自定義析構函數,但不是一個自定義賦值運算符。在你的情況下,這最終導致兩個單獨的LinkedList對象指向相同的節點。

更多信息:https://stackoverflow.com/a/4782927/951890

+0

我寫的自定義賦值運算符,但仍然得到了同樣的錯誤,請檢查在第一篇文章,我編輯了與 – bluedragon

+0

@bluedragon的實現:你還缺少一個拷貝構造函數。 –

+0

好吧我成功(?)寫道,它的工作原理我沒有得到那個錯誤,但在由test2()創建的示例對象總是包含null值,而不是nullptr,因此我的循環變得無限,所以對於晚回覆,我仍然是初學者 – bluedragon