2014-02-09 62 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