无法调整类型为template的堆栈的大小

Cannot resize stack of type template

本文关键字:堆栈 template 调整 类型      更新时间:2023-10-16

我试图调整堆栈的大小,但我的程序在"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结束,并修改您的代码。

你有没有注意到,当你调整数组的大小时,你不会添加新的项目?