【问题标题】:Cannot resize stack of type template无法调整类型模板的堆栈大小
【发布时间】:2014-10-05 14:15:19
【问题描述】:

我正在尝试调整堆栈大小,但我的程序在“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;
}

【问题讨论】:

  • 什么是 T?和大小?
  • 在这种情况下 T 是一个 int 并且 size 默认设置为 10
  • 对于下一个神奇的问题:调试器说什么?
  • 调试器什么也没说。因此,程序终止。 “Stacks.exe 已停止工作”
  • 您正在推送 12 个项目,然后您尝试检索 13:th。

标签: c++ arrays stack


【解决方案1】:

这个错误意味着你在某处有一个超出范围的数组访问,它破坏了 malloc 的数据结构。

错误在于您的推送功能:

template <typename T>
void stack<T>::push(T&& q)
{
    if(topStack < size)
    {
         topStack++;
         stack1[topStack] = q;
    }else{
    resize();
    }
}

当您在调整大小之前写入最后一项时,topStack 比 size 小一,这是一个有效的索引(实际上是最后一个),但随后您将其增加到 size,并将该项写入该位置,这就是不是有效的索引。

顺便说一句,你的班级的索引有很多错误。我的建议是考虑到 c++ 数组从 0 开始并以 size-1 结束,并修改您的代码。

您是否注意到,当您调整数组大小时,您并没有添加新项目?

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2012-11-14
    • 2016-08-20
    • 2023-01-28
    • 1970-01-01
    相关资源
    最近更新 更多