2014-10-05 50 views
-1

我試圖調整我的堆棧大小,但我的程序在'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; 
} 
+0

什麼是T?和尺寸? – 2014-10-05 14:16:17

+0

在這種情況下,T是一個int,默認情況下大小設置爲10 – Nicknack 2014-10-05 14:17:07

+0

,下一個神奇的問題是:調試器說什麼? – 2014-10-05 14:17:45

回答

0

該錯誤意味着您有一個超出數組訪問範圍的地方正在破壞malloc的數據結構。

的錯誤是在你的推送功能:

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

當你寫大小調整前的最後一個項目,topStack是一個小於大小,這是一個有效的指數(最後一個,確實),但隨後你增加它的大小,並寫在該位置的項目,這不是一個有效的索引。

順便說一下,你的班級裏有很多索引錯誤。我的建議是考慮到C++數組從0開始並以大小-1結束,並修改代碼。

你有沒有注意到,當你調整數組的大小時,你不會添加新的項目?

+0

謝謝。將修改我的代碼 – Nicknack 2014-10-05 16:54:50