使用数组的堆栈复制构造函数

时间:2015-10-07 06:18:13

标签: c++

我有一个无法正常工作的复制构造函数。我相信我的逻辑错了,但我不知道在哪里以及为什么。我的代码和我的输出低于。

会员职能:

    Stack300::Stack300(): MAX_STACK(3)
{
    stackArray = new(std::nothrow) Element300[MAX_STACK];
    if(stackArray == NULL)
    {
        cerr << "There is not enough memory for this stack. This stack will not be implemented correctly." << endl;
    }
    else
    {
    top = -1;
    }
}

Stack300::Stack300(const int size):MAX_STACK(size)
{
    stackArray = new Element300[MAX_STACK];
    top = -1;
}

Stack300::Stack300(Stack300& old):MAX_STACK(old.MAX_STACK)
{
    Stack300 tempStack;
    stackArray = new (std::nothrow)Element300[old.MAX_STACK];
    top = -1;
    Element300 temp;
    while(!old.isEmpty()){
        temp = old.pop300();
        tempStack.push300(temp);
    }

    while(!tempStack.isEmpty()){
        temp = tempStack.pop300();
        push300(temp); 
        old.push300(temp); 
    }
}
Stack300::~Stack300()
{
    Element300 tempValue;

    while(!isEmpty())
    {
        tempValue = pop300();
    }

    if(isEmpty())
    {
        delete [] stackArray;
    }
}

void Stack300::push300(const Element300 number)
{
    if(isFull())
    {
        cerr << "The stack is full." << endl << "The push was unsuccessful" << endl;
    }
    else
    {
        top++;
        stackArray[top] = number;
    }
    return;
}
Element300 Stack300::pop300()
{
    Element300 number = 0.0;

    if(isEmpty())
    {
        cerr << "The stack is empty" << endl << "The pop was unsuccessful" << endl;
        return 0.0;
    }
    else
    {
        number = stackArray[top];
        top--;
        return number;
    }
}

void Stack300::viewTB300()
{
    Stack300 tempStack(MAX_STACK);
    Element300 tempValue;

    while(!isEmpty())
    {
        tempValue = pop300();
        tempStack.push300(tempValue);
        cout << tempValue << endl;
    }
    while(!tempStack.isEmpty())
    {
        tempValue = tempStack.pop300();
        push300(tempValue);
    }

    return;
}

void Stack300::viewBT300()
{
    Stack300 tempStack(MAX_STACK);
    Element300 tempValue;

    while(!isEmpty())
    {
        tempValue = pop300();
        tempStack.push300(tempValue);
    }
    while(!tempStack.isEmpty())
    {
        tempValue = tempStack.pop300();
        cout << tempValue << endl;
        push300(tempValue);
    }

    return;

}

bool Stack300::isFull()
{
    bool status;

    if(top == MAX_STACK - 1)
        {
            status = true;
        }
    else
    {
        status = false;
    }

    return status;
}

bool Stack300::isEmpty()
{
    bool status;

    if(top == - 1)
    {
        status = true;
    }
    else
    {
        status = false;
    }

    return status;
}

我的司机:

    using namespace std;

int main ()
{
    Stack300 stack1(6);
    stack1.push300(100);
    stack1.push300(200);
    stack1.push300(300);
    stack1.push300(400);
    stack1.push300(500);
    stack1.push300(600);


    Stack300 stack6(stack1);
    stack6.viewTB300();

    return 0;
}

我目前的输出:

    The stack is full.
The push was unsuccessful
The stack is full.
The push was unsuccessful
The stack is full.
The push was unsuccessful
600
500
400

如果我对此帖有任何其他要求,请告诉我,以便它可以帮助您回答这个问题。

谢谢!

1 个答案:

答案 0 :(得分:1)

您的默认构造函数构造一个最大大小为3的堆栈。然后尝试在其中放入6个元素:

Element300 temp; // MAX_STACK == 3

while(!old.isEmpty()){  // But old has 6 elements
    temp = old.pop300();
    tempStack.push300(temp);
}

此外,您的复制构造函数会清空复制的对象。那些是非常令人困惑的“复制”语义。

您只需将一个堆栈的数据复制到另一个堆栈,而不会弹出或推送。如果您使用std::vector<Element300>进行存储而不是使用自己动态分配的数组,则可以轻松完成此操作。在这种情况下,默认的复制构造函数可以完成这项工作。