我知道那里有很多类似的问题,但我还没有找到任何有用的东西。我已经在这里待了几个小时了,这让我发疯了。当为复制构造函数创建的变量调用析构函数时,出现分段错误。
//Copy Constructor
Stack::Stack(const Stack &aStack)
{
size = 0; //this is incremented as new items are pushed onto the stack.
cap= aStack.cap;
items = new int[aStack.cap]();
for (int i = 0; i < aStack.size; i++)
this->push(aStack.items[i]); //Adds an item if not full and increments size
// I have also tried: items[i] = aStack.items[i]
}
//Destructor
Stack::~Stack()
{
cap= 0;
size= 0;
delete [] items;
items = NULL;
}
我觉得我做错了复制构造函数,但我不知道它是什么。