[英]Template Stack Class with Data types of Linkedlist gives error of Cannot Access Memory at Address c++
这是我的自定义模板堆栈类
#ifndef INC_20F_FLT_PLN_DSSTACK_H
#define INC_20F_FLT_PLN_DSSTACK_H
#include <iostream>
template <typename T>
class DSStack
{
T* data;
int top;
int capacity;
public:
DSStack()=default;
DSStack(int size);
DSStack(const DSStack<T>& copy);
DSStack& operator= (DSStack<T>);
void push(const T& data);
T pop();
T peek();
bool isempty();
bool isfull();
int getsize();
~DSStack(){while ( !isempty() ) {
pop();
}
isempty();};
};
template<typename T>
DSStack<T>::DSStack(int size) {
this->capacity=size;
this->data = new T[capacity];
top=-1;
}
template<typename T>
void DSStack<T>::push(const T& data) {
if(!isfull())
{
this->data[top+1]=data;
top++;
}
}
template<typename T>
T DSStack<T>::pop() {
if(!isempty())
{
top--;
return data[top+1];
}
}
template<typename T>
T DSStack<T>::peek() {
if(!isempty())
{
return data[top];
}
}
template<typename T>
int DSStack<T>::getsize() {
return top+1;
}
template<typename T>
bool DSStack<T>::isempty() {
if(top==-1)
{
return true;
}
else
return false;
}
template<typename T>
bool DSStack<T>::isfull() {
if(top==capacity-1)
{
return true;
}
else
return false;
}
template<typename T>
DSStack<T>::DSStack(const DSStack<T> ©) {
this->data = new T[copy.capacity];
this->capacity = copy.capacity;
for(int i =0;i<capacity;i++)
{
this->data[i]= copy[i];
this->top+=1;
}
}
template<typename T>
DSStack<T> &DSStack<T>::operator=(DSStack<T> copy) {
this->data = new T[copy.capacity];
this->capacity = copy.capacity;
for(int i =0;i<capacity;i++)
{
this->data[i]= copy[i];
this->top+=1;
}
return *this;
}
#endif //INC_20F_FLT_PLN_DSSTACK_H
还有我的模板链表类
#ifndef INC_20F_AUTO_IDX_DSLINKEDLIST_H
#define INC_20F_AUTO_IDX_DSLINKEDLIST_H
#include <cstddef>
#include <utility>
template <typename T>
class DSList
{
struct DSNode
{
T value;
DSNode* prev;
DSNode* next;
DSNode(T value, DSNode *prev = nullptr, DSNode *next = nullptr)
: value{std::move(value)}, prev{prev}, next{next}
{}
DSNode(const DSNode&) = delete;
void operator=(const DSNode&) = delete;
friend void swap(DSNode& a, DSNode& b) {
using std::swap;
swap(a.value, b.value);
swap(a.prev, b.prev);
swap(a.next, b.next);
}
};
public:
DSList() = default;
DSList(const DSList<T>&);
DSList& operator=(DSList<T>);
~DSList();
T const& get(std::size_t pos) const;
T& get(std::size_t pos);
void insert(T value, std::size_t pos);
int getsize();
template<typename U>
friend void swap(DSList<U>&, DSList<U>&);
private:
std::size_t n_elements = 0;
DSNode head = { T{}, nullptr, &tail };
DSNode tail = { T{}, &head, nullptr };
};
template<typename T>
DSList<T>::DSList(const DSList<T>& list)
{
for (auto i = list.n_elements; i > 0; --i) {
insert(list.get(i-1), 0);
}
}
template<typename T>
DSList<T>& DSList<T>::operator=(DSList<T> list)
{
swap(*this, list);
return *this;
}
template<typename T>
DSList<T>::~DSList()
{
for (auto p = head.next; p != &tail; ) {
auto next = p->next;
delete p;
p = next;
}
}
template<typename T>
void swap(DSList<T>& a, DSList<T>& b)
{
using std::swap;
swap(a.head, b.head);
swap(a.tail, b.tail);
swap(a.n_elements, b.n_elements);
}
template<typename T>
const T& DSList<T>::get(std::size_t pos) const
{
auto p = head.next;
while (pos--)
p = p->next;
return p->value;
}
template<typename T>
T& DSList<T>::get(std::size_t pos)
{
auto p = head.next;
while (pos--)
p = p->next;
return p->value;
}
template<typename T>
void DSList<T>::insert(T value, std::size_t pos)
{
auto p = &head;
while (pos--)
p = p->next;
auto next = p->next;
next->prev = p->next = new DSNode(std::move(value), p, next);
++n_elements;
}
template<typename T>
int DSList<T>::getsize() {
return n_elements;
}
#endif //INC_20F_FLT_PLN_DSLINKEDLIST.H
当我试图创建一个 int 类型的堆栈时: DSStack<int>test (2);
,它将允许我推送、弹出、查看数据并返回正确的结果。 但是当我尝试创建链表类型的堆栈时: DSStack<DSList<int>>test2(2);
,我还将创建一个整数DSList<int>numList;
我可以将一些数字放入链表中,它不会出错。 但是,当我尝试将我创建的链表推送到堆栈中时,它会给出以下错误:进程已完成,退出代码为 -1073740940 (0xC0000374)。 使用调试器运行会给我带来无法在随机位置访问内存的错误。 该行指向链表类的析构函数,但我相信链表类的析构函数确实正常工作。 我想知道这是否与我如何设置堆栈类有关。 我真的很想得到一些关于如何解决这个问题的帮助。 谢谢!
好的,你的列表实现中有一堆奇怪的东西。 我能够得到以下单链表的工作
#ifndef INC_20F_AUTO_IDX_DSLINKEDLIST_H
#define INC_20F_AUTO_IDX_DSLINKEDLIST_H
#include <cstddef>
#include <utility>
template <typename T>
class DSList
{
struct DSNode
{
T value;
DSNode* next;
DSNode(T value, DSNode *next = nullptr)
: value{std::move(value)}, next{next}
{}
DSNode(const DSNode&) = delete;
void operator=(const DSNode&) = delete;
friend void swap(DSNode& a, DSNode& b) {
using std::swap;
swap(a.value, b.value);
swap(a.prev, b.prev);
swap(a.next, b.next);
}
};
public:
DSList() = default;
DSList(const DSList<T>&);
DSList& operator=(DSList<T>);
~DSList();
T const& get(std::size_t pos) const;
T& get(std::size_t pos);
void insert(T value);
int getsize();
template<typename U>
friend void swap(DSList<U>&, DSList<U>&);
private:
DSNode* head = nullptr;
};
template<typename T>
DSList<T>::~DSList()
{
if (head == nullptr) return;
auto current = head;
while(current->next != nullptr){
auto next = current->next;
delete current;
current = next;
}
}
template<typename T>
void DSList<T>::insert(T value)
{
auto new_node = new DSNode(std::move(value));
if (head == nullptr){
head = new_node;
return;
}
auto p = head;
while (p->next != nullptr)
p = p->next;
auto node = new DSNode(std::move(value));
p->next = node;
}
#endif //INC_20F_FLT_PLN_DSLINKEDLIST.H
int main(){
DSList<int> here;
here.insert(1);
std::cout << "Here";
}
在创建 DLL 之前,先创建一个 SLL,并确保它正常工作。 当我插入时,当列表被破坏时,您的访问发生了一些奇怪的事情。 我修剪了这个特定解决方案不需要的所有东西。 您将自行添加计数行为和批量插入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.