2014-02-09 61 views
0

我創建了自己的堆棧和重載函數。然而,當我調用函數返回堆棧被損壞,我不明白爲什麼:/我是新來的C + +,並希望學習!這裏是我的代碼堆棧損壞,不會返回

主要

int main(){ 
string line; 
SStack s1(1000); 
SStack s2(1000); 
int cap = s1.getCapacity(); 
cout << "Is the stack empty? " << s1.IsEmpty() << "\n"; 
cout << "The capacity of the stack is: " << cap << "\n"; 
ifstream myfile("all.last.txt"); 
cout << "s1 begin pushing: \n"; 
for (int i = 0; i <= 500; i++){ 
    getline(myfile, line); 
    cout << "Pushing " << line << "\n"; 
    s1.push(line); 
} 
cout << "s2 begin pushing: \n"; 
for (int i = 0; i <= 50; i++){ 
    getline(myfile, line); 
    cout << "Pushing " << line << "\n"; 
    s2.push(line); 
} 
myfile.close(); 
cout << "Is the stack empty? " << s1.IsEmpty() << "\n"; 
string top = s1.top(); 
cout << "The top object on the stack is: " << top << "\n"; 
cout << "The size of the stack is: " << s1.size() << "\n"; 
cout << "Popping: " << s1.pop() << "\n"; 
cout << "Size after pop is: " << s1.size() << "\n"; 
s1 = s1 + s2; 
cout << s1.top(); 

}

SStack功能,這並不返回

SStack::SStack(const SStack& s) : used(-1), Capacity(0), DynamicStack(0){ 
    *this = s; 
} 

SStack SStack::operator=(const SStack& s){ 
    if (this != &s){ 
     int cap = s.getCapacity(); 
     DynamicStack = new string[cap]; 
     Capacity = cap; 
     used = -1; 
     for (int count = 0; count < s.size(); count++){ 
      DynamicStack[count] = s.DynamicStack[count]; 
      used++; 
     } 
     } 
     return *this; 
} 

SStack SStack::operator +(const SStack& s2){ 
int size1 = used + 1; 
int size2 = s2.size(); 
SStack result = *this; 
if (size1 + size2 <= Capacity){ 
    for (int count = 0; count < s2.size(); count++){ 
     result.push(s2.DynamicStack[count]); 
    } 
    return result; 
} 
else{ 
    cout << "Error stack is not big enough"; 
    return result; 
} 
+6

請將此減少到要求的最小*程序以演示此問題。 –

+2

使用調試器,設置斷點,觀察變量。不要只是轉儲堆的代碼。請參閱[本網站](http://stackoverflow.com/help/how-to-ask)以瞭解如何針對SO提出優秀問題。 –

+0

......幷包括所有需要的,例如類定義(「SStack.h」)丟失。 –

回答

0

您在2個​​地方分配堆疊的情況下,但我還沒有看到運營商=定義。 我建議你添加一個operator =()到你的SStack類並委託你的拷貝構造函數到這個運算符=()。請確保您的運營商=()檢查它是否將自身複製:

... 
SStack::SStack(const SStack &s) : used(-1), Capacity(0), DynamicStack(0) 
{ 
    *this = s; 
} 
SStack &SStack::operator=(const SStack &s) 
{ 
    if (this != &s && s.size() > 0) 
    { 
     // check if we already have an allocated array 
     if (Capacity < s.size()) 
     { 
      // safe even if Capacity==0 as long as DynamicStack==0 too 
      delete [] DynamicStack; 

      int cap = s.getCapacity(); 
      DynamicStack = new string[cap]; 
      Capacity = cap; 
     } 
     used = -1; 
     for (int count = 0; count < s.size(); count++){ 
      DynamicStack[count] = s.DynamicStack[count]; 
      used++; 
     } 
    } 
    return *this; 
} 

與degugger試試這個,我建議你重新檢查你「拿來主義」的變量,以確保它實際上有你在所有地方想索引呢用來。

希望這會有所幫助。

+0

複製操作是新的,就像在初始化? – bengalman430

+0

我添加了一個編輯來澄清,您在那裏初始化您的變量,以便在您輸入operator =()函數時它們的值不會被定義。複製操作與您在當前的複製構造函數中所做的一樣,只不過是另外一個。運算符=()應檢查當前的分配,容量等,並釋放所有內容,以便在當前空間不足時分配足夠的空間來複制整個外部堆棧。 – DNT

+0

好吧,我現在就試試 – bengalman430