0 c++ visual-c++ data-structures c++11
我创建了这个链表类来在我的工作中使用它,但我遇到了这个异常“game.exe 中 0x003216D2 处的第一次机会异常:0xC0000005:访问冲突写入位置 0x00000004”。它来自擦除功能,有人可以告诉我如何修复它并解释问题所在
#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
Run Code Online (Sandbox Code Playgroud)
访问冲突(在 Windows 中,或在 unice 中为分段错误)表示您尝试错误地访问内存。您尝试访问进程空间之外的内存,或者尝试了错误的访问类型。
该错误消息还告诉您尝试访问的地址是什么以及尝试的访问类型。您尝试写入内存地址 0x00000004,即距第 0 个地址 4 个字节。看起来您试图通过空指针进行写入,并且写入对象是距离对象开头 4 个字节的成员。
虽然并不总是能够确定真正的原因是什么,但这就是它的样子。调试问题的最简单方法是在调试器中运行应用程序,并让调试器告诉您访问冲突在哪里。然后检查程序的状态并尝试确定它是如何在这种情况下结束的。