无法调整类型为template的堆栈的大小
Cannot resize stack of type template
我试图调整堆栈的大小,但我的程序在"cout"之后一直终止。在输出端,它显示1,然后程序终止。在这种情况下,T是一个int,默认情况下大小设置为10。任何帮助都将不胜感激。
#include <iostream>
#include <fstream>
using namespace std;
template <typename T>
class stack {
public:
int topStack;
T* stack1;
int size;
void copy(const stack& other);
void move(stack&& other);
// constructor
stack ();
// destructor
~stack()
{
delete[] stack1;
};
// copy constructor
stack (const stack&);
// copy assignment
stack& operator= (const stack&);
// move constructor
stack (stack&&);
// move assignment
stack& operator= (stack&&);
T& top() const; // return the top element
void pop (); // remove the top element
void push(const T&); // add element on top of stack
void push (T&&); // add element on top of stack
bool empty() const; // is the stack empty?
void clear(); // remove all elements
ostream& print(ostream&, stack&);
void resize();
};
//Default Constructor
template <typename T>
stack<T>::stack()
{
size=10;
stack1= new T[size];
for(int b =0; b < size; b ++)
{
stack1[b] = T();
}
topStack =-1;
}
//Copy Constructor
template <typename T>
void stack<T>::copy(const stack& other)
{
topStack = other.topStack;
stack1= new T[other.size];
size =other.size;
for(int i=0; i< other.size ; i++)
{
stack1[i]=other.stack1[i];
}
}
//Copy assignment
template <typename T>
stack<T>& stack<T>::operator =(const stack& other)
{
if (this == &other) return *this;
T* store = new T[other.size];
for(int g =0; g < other.size ; g++)
{
store[g]= other.stack1[g];
}
delete[] stack1;
this->stack1 = store;
this->size = other.size;
this-> topStack = other.topStack;
return *this;
}
//Move Constructor
template<typename T>
void stack<T>::move(stack && other)
{
topStack = other.topStack;
other.topStack = 0;
stack1 = other.stack1;
for(int u =0; u < other.size ; u++)
{
other.stack1[u]=0;
}
size = other.size;
other.size=0;
}
//Move assignment
template <typename T>
stack<T>& stack<T>::operator= (stack&& other)
{
this->size = other.size;
other.size=0;
this->topStack = other.topStack;
other.topStack=0;
this->stack1 = other.stack1;
for(int u =0; u < this->size ; u++)
{
other.stack1[u]=0;
}
return *this;
}
//Checks if stack is empty
template <typename T>
bool stack<T>::empty() const
{
return topStack == -1;
}
//Resize array
template<typename T>
void stack<T>::resize()
{
cout << "DAYYYY55UM";
T* storage = new T[this->size*2];
cout << "DAYYYYUM";
for(int r=0; r < this->size ; r++)
{
storage[r]= this->stack1[r];
}
delete[] this->stack1;
this->stack1= storage;
this->size = size*2;
cout << "DAYYYYUM";
}
//Returns top
template <typename T>
T& stack<T>::top() const
{
if(empty())
{
cout << "ERROR: Stack is empty. "<< endl;
return;
//Make a throw catch statement here
}
return stack1[topStack];
}
//Pop
template <typename T>
void stack<T>::pop()
{
if(empty())
{
cout << "ERROR: Stack is empty." << endl;
return;
}
stack1[topStack] =0;
topStack--;
}
//Push
template <typename T>
void stack<T>::push(const T& q)
{
if(topStack < size)
{
topStack++;
stack1[topStack] = q;
}else{
resize();
}
}
//Push
template <typename T>
void stack<T>::push(T&& q)
{
if(topStack < size)
{
topStack++;
stack1[topStack] = q;
}else{
resize();
}
}
//Print Function
template <typename T>
ostream& stack<T>::print(ostream& os, stack& other)
{
os << other.stack1 ;
}
template <typename T>
void stack<T>::clear()
{
for(int g=0; g < size; g++ )
{
stack1[g]=0;
}
}
int main()
{
stack<int> world;
world.push(9);
world.push(40);
world.push(40);
world.push(9);
world.push(40);
world.push(40);
world.push(9);
world.push(40);
world.push(40);
world.push(9);
world.push(40);
world.push(40);
cout << world.stack1[12] << endl;
return 0;
}
该错误意味着您在某个地方进行了越界数组访问,从而损坏了malloc的数据结构。
错误出现在您的推送功能中:
template <typename T>
void stack<T>::push(T&& q)
{
if(topStack < size)
{
topStack++;
stack1[topStack] = q;
}else{
resize();
}
}
当你在调整大小之前写最后一个项目时,topStack比size小一个,这是一个有效的索引(实际上是最后一个),但你把它增加到size,然后在那个位置写项目,这不是一个有效索引。
顺便说一句,在类的索引中有很多错误。我的建议是考虑到c++数组从0开始,到1结束,并修改您的代码。
你有没有注意到,当你调整数组的大小时,你不会添加新的项目?
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 算法问题:查找从堆栈中弹出的所有序列
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 传递给std::function template的template参数究竟代表什么
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 在template中使用std::variant的template函数
- 为什么调用堆栈数组会导致内存泄漏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 我的 int main() 中出现堆栈溢出错误
- 堆栈和队列是否像C++中的数组一样传递?
- 拥有映射的现代方法,该映射可以指向或引用已在堆栈上分配的不同类型的数据
- 为什么 STL 容器适配器堆栈中的 top 返回常量引用?
- 从堆栈分配的原始指针构造智能指针
- 在函数范围内在堆栈上分配的数组在离开函数时是否总是被释放?
- 堆栈中大小变量输入错误 (C++)
- 堆栈问题(平衡表达式问题集)
- C++ 在堆栈中包含多态属性的类对象存储
- 通过"Value Template Argument"与常规阵列在堆栈中分配内存
- 无法调整类型为template的堆栈的大小