2013-10-15 16 views
1

我一直在我的代碼得到一個分段錯誤我正在與我的分段錯誤循環

頁眉

#ifndef DUALSTK 
    #define DUALSTK 

    #include <deque> 

    using namespace std; 

    enum stackNumber {One, Two}; 


    template <typename T> 
    class dualStack { 
     public: 
     // constructor. set counts to 0 
      dualStack() : dualStackElements(20, 0) {} 

      void push(const T& item, stackNumber n); 
      void pop(stackNumber n); 

      T& top(stackNumber n); 
      //const T& top(stackNumber n) const; 

      bool empty(stackNumber n) const; 
      int size(stackNumber n) const; 

      int getCount1() const {return count1;} 
      int getCount2() const {return count2;} 

     private: 
      deque<T> dualStackElements; 
      int count1, count2; 
    }; 

    #endif // DUALSTK 

的main.cpp

#include <iostream> 
    #include <deque> 
    #include "dualstk.h" 

    using namespace std; 

    template <typename T> 
    T& dualStack<T>::top(stackNumber n) { 
     int num = 0; 
     if (n == 0) { 
      num = count1 - 1; 
      return num; 
     } 

     if (n == 1) { 
      num = 20 - count2; 
      return num; 
     } 
    } 


    template <typename T> 
    void dualStack<T>::push(const T& item, stackNumber n) { 
     if (n == 0) { 
      count1++; 
      dualStackElements[top(One)] = item; 
     } 

     if (n == 1) { 
      count2++; 
      dualStackElements[top(Two)] = item; 
     } 
    } 

    template <typename T> 
    void dualStack<T>::pop(stackNumber n) { 
     int item = 0; 
     int item2 = 0; 
     if (n == 0) { 
      item = dualStackElements[top(One)]; 
      cout << item << " "; 
      count1--; 
     } 

     if (n == 1) { 
      item2 = dualStackElements[top(Two)]; 
      cout << item2 << " "; 
      count2--; 
     } 
    } 

    template <typename T> 
    bool dualStack<T>::empty(stackNumber n) const { 

return 1; 
    } 



    int main() { 

     dualStack<int> stack; 

     for(int i = 1; i < 21; i++) { 
      if(i % 2 == 0) { 
       stack.push(i, One); 
      } 
      else { 
       cout << i; 
       stack.push(i, Two); 
      } 
     } 

     cout << endl; 


     for (int j = 0; j < 10; j++) 
      stack.pop(One); 

     cout << endl; 

     for (int i = 0; i < 10; i++) 
      stack.pop(Two); 

     cout << endl; 

     cout << stack.getCount2(); 
     cout << stack.getCount1(); 

     cout << endl; 


     cout << "\n" << stack.top(One); 
     cout << stack.top(Two) << endl; 

     return 0; 
    } 

每當我有一個循環爲我的stack.pop它將正常工作 ,但如果我做一個for循環後,我使用一個做stack.pop它不會工作

我不知道我做錯了什麼這

 for (int j = 0; j < 10; j++) 
      stack.pop(One); 


     for (int i = 0; i < 10; i++) 
      stack.pop(Two); 
+0

請注意,要使模板有用,必須在頭中完全聲明(包含函數體)。 –

+0

我是否缺少模板,無用? @AlexisWilke – UnhinderedLimpidity

回答

0

我想你是誤會這是什麼一樣(或我誤解你的代碼):

dualStackElements[top(One)] = item; 

你還沒有宣佈dualStackElements數組

而不是聲明兩個出列。

deque<T> dualStackElementsOne; 
deque<T> dualStackElementsTwo; 
+0

沒有。 myDeque [n]爲你提供deque中的元素n。 http://www.cplusplus.com/reference/deque/deque/operator[]/ – Raja

0

您的Top方法需要一些錯誤檢查。如果count1爲0(或count2> 20)會怎麼樣?您會返回一個負值,然後將其用作您的雙端隊列的索引。並通過參考刪除返回!

1

函數top()返回對局部變量'num'的引用。它的存儲位於堆棧中,因此當函數返回時值將被修改,然後將其用作dualStackElements的索引。這可能導致訪問無效的內存地址。

+0

我不知道我需要做什麼來解決這個問題。任何建議? @tristan – UnhinderedLimpidity

+0

在你的情況下,函數返回一個&T,而不是int。但你期望一個int?因爲它用於索引deque ...如果你真的想要一個int,你可以返回一個int而不是一個引用? – tristan

0
  1. top()方法返回對方法調用後釋放的局部變量num的引用。但是不同的操作系統對訪問非法內存有不同的限制,有時候,在Linux中就可以。

  2. 在你的構造函數中,沒有對字段進行初始化操作:count1,count2。該程序總是返回「分段錯誤;」如果我註釋掉「count1 = count2 = 0;」在我修改過的構造函數中。

  3. 爲了得到一個程序的穩定結果,你應該在make之前做一個清理。我不知道爲什麼,但它確實發生在我的測試中。