所以我一直在試圖實現一個arrayStack,但我似乎有彈出超過100個元素的問題。我的程序在達到100個時會停止工作。這似乎是推動和流行的問題,但不太明白我的問題是什麼。這裏我的ArrayStack發生了什麼?
#ifndef _ARRAYSTACK_1_HPP_
#define _ARRAYSTACK_1_HPP_
#include <stddef.h>
#include "StackADT.h"
#define DEFAULT_SIZE 50
template<class T>
class ArrayStack_1 : public StackADT<T> {
private:
T *arrayStack;
int index;
int maxSize;
public:
// a constructor for the arrayStack that creates and arrayStack of a given size
ArrayStack_1(int size = DEFAULT_SIZE) {
maxSize = size;
index = 0;
arrayStack = new T[size];
}
public:
// a constructor for the arrayStack that creates and arrayStack of a given size
ArrayStack_1(int size = DEFAULT_SIZE) {
maxSize = size;
index = 0;
arrayStack = new T[size];
}
//a destructor that deletes the arrayStack
~ArrayStack_1() {
delete[] arrayStack;
}
bool Empty() {
return index==0;
}
void Push(T& item = 0) {
if (index<maxSize) {
arrayStack[index]= item;
index+=1;
}else {
T* tempArrayStack = arrayStack; //making a copy of the array stack
maxSize+=1;
arrayStack = new T[maxSize];
for (int i=0; i<maxSize; i++){
arrayStack[i]=tempArrayStack[i];
}
arrayStack[index]=item;
index+=1;
delete[] tempArrayStack;
}
}
T Pop() {
if(arrayStack[index]==0){
return 0;
}else {
T element = arrayStack[index];
index--;
return element;
}
}
}
你可以顯示你從運行你的代碼得到的實際錯誤消息嗎? –
它結束之前,即使它說數組增長爲1. http://imgur.com/rf6GQ6p – dikshant
當人們爲您修復代碼時,您需要停止編輯您的問題。答案不再有意義,因爲問題已經改變。 – John3136