我自己写了一个模拟栈的程序,是用数组实现的。
程序能够运行并能正常显示,但却在最后的system("pause")报错,求大神帮帮忙。
代码如下:
class Stack {
public:
int* arrays = new int[_capacity];
Stack() {
for (int i = 0; i < _capacity;i++)
{
arrays[i] = 0;
}
};
void push_back(int value) {
index++;
if (index>=_capacity)
{
int* newarray = new int[_capacity + 5];
for (int i = 0; i++; i < _capacity)
{
newarray[i] = arrays[i];
}
delete[]arrays;
arrays = nullptr;
arrays = newarray;
delete[]newarray;
newarray = nullptr;
_capacity += 5;
setDefault(length);
}
arrays[index] = value;
length++;
}
void print() {
for (int i = 0; i < _capacity; i++)
{
cout << arrays[i] << "";
}
}
void pop_back() {
if (index >= 0)
{
arrays[index] = 0;
index--;
length--;
}
}
int size() {
return length;
}
int capacity() {
return _capacity;
}
private:
int index = -1;
int length = 0;
int _capacity = 10;
void setDefault(int startIndex) {
for (int i = startIndex; i < _capacity; i++)
{
arrays[i] = 0;
}
}
};
int main(){
Stack* stack = new Stack();
stack->push_back(56);
stack->print();
cout << endl;
stack->push_back(39);
stack->print();
cout << endl;
stack->pop_back();
stack->print();
cout << endl;
system("pause");//当我按下任意一个键时这里出现了问题
}
![图片说明](https://img-ask.csdn.net/upload/201908/15/1565848929_171163.jpg)
这该怎么办呢,求大神解决,!
谢谢!