Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自定义链表创建RtlValidateHeap错误,结构具有链表_C++_Class_Linked List_Heap Memory_Singly Linked List - Fatal编程技术网

C++ 自定义链表创建RtlValidateHeap错误,结构具有链表

C++ 自定义链表创建RtlValidateHeap错误,结构具有链表,c++,class,linked-list,heap-memory,singly-linked-list,C++,Class,Linked List,Heap Memory,Singly Linked List,这是我正在处理的LinkedList实现。它适用于任何数据类型,但当我尝试创建具有链表的类型的链表时,问题就出现了 关于使用VisualStudio进行调试,我得到了 为RtlValidateHeap(00790000,007B16D0)指定的地址无效 代码如下所示: typedef unsigned long int LENGTH_T; template < typename type > struct nodes { type _value; nodes<t

这是我正在处理的LinkedList实现。它适用于任何数据类型,但当我尝试创建具有链表的类型的链表时,问题就出现了 关于使用VisualStudio进行调试,我得到了 为RtlValidateHeap(00790000,007B16D0)指定的地址无效

代码如下所示:

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 );
}
typedef无符号长整型长度\u T;
模板<类型名称类型>
结构节点
{
类型_值;
节点*\u下一个\u节点;
nodes(){u next_node=nullptr;}
};
模板<类型名称类型>
类链接列表
{
节点*_元素_节点;
长度T尺寸;
公众:
节点*节点处(长度处);
类型和运算符[](长度处);
无效推回(常数型src);
长度大小()常量{return大小;}
LinkedList();
~LinkedList();
};
模板
nodes*LinkedList::node_at(LENGTH_T at){
如果(at==0)
返回元素节点;
else if(在>|u size-1 ||u size==0时){
打印错误(“尝试访问超出范围”);
}
//用于存储顺序节点的tmp节点
节点*cur\u tmp\u node\u ptr=\u elem\u nodes->\u next\u node;
对于(大小i=1;i_next_node;
返回当前tmp节点ptr;
}
模板
类型和链接列表::运算符[](长度为)
{
返回节点_at(at)->_值;
}
模板
void LinkedList::push_back(常量类型src)
{
如果(_size==0){
_元素节点->\u值=src;
_大小++;
}
否则{
节点*新节点=新节点;
新节点->\u值=src;
新建\u节点->\u下一个\u节点=nullptr;
节点在(\u大小-1)->\u下一个\u节点=新的\u节点;
_大小++;
}
}
模板
LinkedList::LinkedList()
{
_尺寸=0;
_元素节点=新节点;
_元素节点->元素值=类型();
_elem_nodes->_next_node=nullptr;
}
模板
LinkedList::~LinkedList()
{
如果(_size>1)//当size=0时,那么_size-1=-1,但是_size是无符号的;
对于(长度i=\u大小-1;i>0;i--){
删除(第(i)项中的节点_);
}
删除(元素节点);
}
下面是一个可以观察到指定问题的代码示例 f、 e

struct测试{
int任何东西;
};
结构测试2{
链接列表t;
};
int main()
{
链接列表t;
t、 向后推(test2());
t、 向后推(test2());
返回0;
} 
**编辑:我编写了自定义赋值运算符和复制构造函数,不再出现该错误,但在上面的test2().t示例中,_next_节点总是包含垃圾值,而不是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;
    }
}
模板
LinkedList和LinkedList::operator=(常量LinkedList和其他)
{
如果(&其他==此)
归还*这个;
此->~LinkedList();
此->\u元素\u节点=空ptr;
_尺寸=0;
节点*cur\u this\u节点=this->\u元素节点;
节点*cur_other_节点=其他。_元素_节点;
while(cur_other_node!=nullptr)
{
cur_this_node=新节点;
cur\u此节点->\u值=cur\u其他节点->\u值;
这个->_size++;
cur\u this\u node=cur\u this\u node->\u next\u node;
cur\u other\u node=cur\u other\u node->\u next\u node;
}
归还*这个;
}
模板
LinkedList::LinkedList(常量LinkedList和其他)
{
_尺寸=0;
节点*cur\u this\u节点=this->\u元素节点;
cur_this_node=nullptr;
节点*cur_other_节点=其他。_元素_节点;
while(cur_other_node!=nullptr)
{
cur_this_node=新节点;
cur\u此节点->\u值=cur\u其他节点->\u值;
cur_this_node->_next_node=nullptr;
这个->_size++;
cur\u this\u node=cur\u this\u node->\u next\u node;
cur\u other\u node=cur\u other\u node->\u next\u node;
}
}
您违反了三条(或四条或五条)规则。您已定义自定义析构函数,但未定义自定义赋值运算符。在您的例子中,这最终会导致两个单独的LinkedList对象指向相同的节点

更多信息:

您违反了三条(或四条或五条)规则。您已定义自定义析构函数,但未定义自定义赋值运算符。在您的例子中,这最终会导致两个单独的LinkedList对象指向相同的节点


更多信息:

我编写了自定义赋值运算符,但仍然出现相同的错误。请检查,在第一篇文章中,我使用that@bluedragon:您还缺少一个复制构造函数。好的,我成功地(?)编写了该构造函数,并且它可以工作。我没有收到该错误,但在test2()创建的示例对象中总是包含null值,而不是null ptr,因此我的循环是无限的,所以对于延迟回复,我仍然是一个新手。我编写了自定义赋值运算符,但仍然得到相同的错误。请检查,在第一篇文章中,我使用that@bluedragon:您还缺少一个副本构造函数。好的,我成功(?)写了这个,它就工作了,我没有得到那个错误,但在示例中,test2()创建的对象总是包含null值,而不是null ptr,因此我的循环是无限的,所以对于后期的回复,我仍然是一个新手
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;
    }
}