C++ 0xC0000005:访问冲突写入位置0x00000004.&x27;
我创建这个链表类是为了在我的工作中使用它,但是我有一个异常“第一次机会异常在game.exe中的0x003216D2:0xC0000005:访问冲突写入位置0x00000004”。它来自擦除函数。有人能告诉我如何修复它并解释它是什么问题吗C++ 0xC0000005:访问冲突写入位置0x00000004.&x27;,c++,visual-c++,c++11,data-structures,C++,Visual C++,C++11,Data Structures,我创建这个链表类是为了在我的工作中使用它,但是我有一个异常“第一次机会异常在game.exe中的0x003216D2:0xC0000005:访问冲突写入位置0x00000004”。它来自擦除函数。有人能告诉我如何修复它并解释它是什么问题吗 #ifndef LINKEDLIST_HPP #define LINKEDLIST_HPP #include <vector> #include <cassert> template<typename T> class
#ifndef LINKEDLIST_HPP
#define LINKEDLIST_HPP
#include <vector>
#include <cassert>
template<typename T>
class Linkedlist
{
private:
// The basic doubly linked Linkedlist node.
// Nested inside of Linkedlist, can be public
// because the Node is itself private
struct Node
{
T _data;
Node *_prev;
Node *_next;
Node( const T & d = T(), Node * p = NULL, Node * n = NULL )
: _data( d ), _prev( p ), _next( n ) { }
// Rvalue
Node(Node && n)
{
_data = std::move(n._data);
_prev = std::move(n._prev);
_next = std::move(n._next);
}
// = operator
Node& operator =(Node && n)
{
_data = std::move(n._data);
_prev = std::move(n._prev);
_next = std::move(n._next);
return *this;
}
};
public:
class LinkedListIter : public std::iterator<std::forward_iterator_tag , T , int>
{
Node* _node;
public:
LinkedListIter(Node* p=nullptr) : _node(p){}
~LinkedListIter(){}
Node* getNode(){return _node;}
T operator * () { return _node->_data; }
LinkedListIter & operator ++()
{
_node = _node->_next;
return *this;
}
LinkedListIter operator ++(int)
{
LinkedListIter retVal = *this;
++this;
return retVal;
}
bool operator < (LinkedListIter const& rhs) const
{
return _Node < rhs._Node;
}
bool operator != (LinkedListIter const& rhs) const
{
return _Node != rhs._pNode;
}
bool operator == (LinkedListIter const& rhs) const
{
return _Node == rhs._Node;
}
};
Linkedlist( )
{ init(); }
~Linkedlist()
{
clear();
delete _head;
delete _tail;
}
Linkedlist(const Linkedlist & rhs)
{
init( );
*this = rhs;
}
const Linkedlist & operator= (const Linkedlist & rhs)
{
if( this == &rhs )
return *this;
clear( );
for(const_iterator itr = rhs.begin(); itr != rhs.end( ); ++itr)
push_back(*itr);
return *this;
}
// Return iterator representing beginning of Linkedlist.
// Mutator version is first, then accessor version.
LinkedListIter begin()
{ return LinkedListIter(_head->_next);}
// Return iterator representing endmarker of Linkedlist.
// Mutator version is first, then accessor version.
LinkedListIter end()
{ return LinkedListIter(_tail); }
// Return number of elements currently in the Linkedlist.
int size() const
{ return _size; }
// Return true if the Linkedlist is empty, false otherwise.
bool empty() const
{ return size() == 0; }
void clear()
{
while( !empty() )
pop_front();
}
// front, back, push_front, push_back, pop_front, and pop_back
// are the basic double-ended queue operations.
T & front()
//{ return *begin( ); }
{return _head->_next.data}
const T & front() const
{ return *begin(); }
T & back( )
{ return *--end(); }
const T & back() const
{ return *--end(); }
void push_front(const T & x)
{ insert( begin(), x ); }
void push_back(const T & x)
{ insert( end(), x ); }
void pop_front()
{ erase(begin()); }
void pop_back()
{ erase(--end()); }
// Insert x before itr.
LinkedListIter insert(LinkedListIter& itr, const T & x)
{
Node *p = new Node(x,itr.getNode()->_prev,itr.getNode());
_size++;
return LinkedListIter(p->_prev = p->_prev->_next = new Node(x, p->_prev, p));
}
// Erase item at itr.
LinkedListIter erase(LinkedListIter itr)
{
Node *p = itr.getNode();
LinkedListIter retVal(p->_next);
p->_prev->_next = p->_next;
p->_next->_prev = p->_prev;
delete p;
_size--;
return retVal;
}
LinkedListIter erase(LinkedListIter start, LinkedListIter end)
{
for(iterator itr = start; itr != end;)
itr = erase(itr);
return end;
}
private:
int _size;
Node *_head;
Node *_tail;
void init()
{
_size = 0;
_head = new Node;
_tail = new Node;
_head->_next = _tail;
_tail->_prev = _head;
}
};
#endif
\ifndef LINKEDLIST\u水电站
#定义LINKEDLIST_HPP
#包括
#包括
模板
类链接列表
{
私人:
//基本的双链接Linkedlist节点。
//嵌套在Linkedlist中,可以是公共的
//因为节点本身是私有的
结构体类型
{
T_数据;
节点*_prev;
节点*\u下一步;
节点(常数T&d=T(),节点*p=NULL,节点*n=NULL)
:_data(d),_prev(p),_next(n){
//右值
节点(节点(&n)
{
_数据=标准::移动(n.\u数据);
_prev=std::move(n.\u prev);
_下一步=标准::移动(n.\u下一步);
}
//=操作员
节点和运算符=(节点和运算符(&n)
{
_数据=标准::移动(n.\u数据);
_prev=std::move(n.\u prev);
_下一步=标准::移动(n.\u下一步);
归还*这个;
}
};
公众:
类LinkedList:public std::iterator
{
节点*\u节点;
公众:
LinkedList(Node*p=nullptr):\u Node(p){}
~LinkedLister(){}
Node*getNode(){return\u Node;}
T运算符*(){return\u node->\u data;}
LinkedLister和运算符++()
{
_node=\u node->\u next;
归还*这个;
}
LinkedLister运算符++(int)
{
LinkedLister retVal=*此;
++这,;
返回返回;
}
布尔运算符<(链接列表常量和rhs)常量
{
返回_节点<右侧。_节点;
}
布尔运算符!=(链接列表常量和rhs)常量
{
返回_节点!=rhs._节点;
}
布尔运算符==(LinkedLister常量和rhs)常量
{
返回_Node==rhs._Node;
}
};
链接列表()
{init();}
~Linkedlist()
{
清除();
删除标题;
删除尾;
}
链接列表(常量链接列表和rhs)
{
init();
*这=rhs;
}
常量链接列表和运算符=(常量链接列表和rhs)
{
如果(此==&rhs)
归还*这个;
清除();
for(常量迭代器itr=rhs.begin();itr!=rhs.end();+itr)
推回(*itr);
归还*这个;
}
//返回表示Linkedlist开头的迭代器。
//首先是Mutator版本,然后是accessor版本。
LinkedLister开始()
{返回LinkedList(_head->_next);}
//返回表示Linkedlist的endmarker的迭代器。
//首先是Mutator版本,然后是accessor版本。
LinkedListEnd()
{返回链接列表(_tail);}
//返回Linkedlist中当前的元素数。
int size()常量
{返回_size;}
//如果Linkedlist为空,则返回true,否则返回false。
bool empty()常量
{返回大小()==0;}
无效清除()
{
而(!empty())
pop_front();
}
//前、后、前推、后推、前弹出、后弹出
//是基本的双端队列操作。
T&front()
//{return*begin();}
{return\u head->\u next.data}
常数T&前()常数
{return*begin();}
T&back()
{return*--end();}
常量T&back()常量
{return*--end();}
无效推压前(常数T&x)
{插入(begin(),x);}
无效推回(常数T&x)
{insert(end(),x);}
void pop_front()
{擦除(begin());}
void pop_back()
{擦除(--end());}
//在itr之前插入x。
LinkedLister插入(LinkedLister&itr、常量T&x)
{
Node*p=新节点(x,itr.getNode()->\u prev,itr.getNode());
_大小++;
返回LinkedList(p->_prev=p->_prev->_next=new Node(x,p->_prev,p));
}
//在itr处擦除项目。
LinkedLister擦除(LinkedLister itr)
{
Node*p=itr.getNode();
LinkedLister检索(p->u下一步);
p->_上一个->_下一个=p->_下一个;
p->_next->_prev=p->_prev;
删除p;
_大小--;
返回返回;
}
LinkedLister擦除(LinkedLister开始,LinkedLister结束)
{
for(迭代器itr=start;itr!=end;)
itr=擦除(itr);
返回端;
}
私人:
内部尺寸;
节点*头;
节点*_尾;
void init()
{
_尺寸=0;
_头=新节点;
_tail=新节点;
_头->下一步=\u尾;
_尾部->前部=\u头部;
}
};
#恩迪夫
您应该检查指针。尝试仔细调试擦除功能
访问冲突
错误通常发生在指针指向“外部”内存区域(即应用程序不保留的内存)时 你应该检查你的指针。尝试仔细调试擦除功能
访问冲突
错误通常发生在指针指向“外部”内存区域(即应用程序不保留的内存)时 你应该检查你的指针。尝试仔细调试擦除功能
访问冲突
错误通常发生在指针指向“外部”内存区域(即应用程序不保留的内存)时 你应该检查你的指针。尝试仔细调试擦除功能
访问冲突
错误通常发生在指针指向“外部”内存区域(即应用程序不保留的内存)时 访问冲突(在windows中,或unices中的分段错误)表示您试图错误地访问内存。