【问题标题】:Copy Constructor and/or pop method in framework for a stack class C++堆栈类 C++ 框架中的复制构造函数和/或弹出方法
【发布时间】:2013-09-12 00:48:05
【问题描述】:

所以我有一个由随机生成的字符串组成的堆栈,然后是一个指向我的 .cpp 代码中下一项的指针。然后我检查并从堆栈中弹出每个项目并将它们打印出来。虽然最后我得到了一个段错误,所以我猜我正在尝试将一个项目弹出一个超出我不拥有内存的堆栈。

我认为我的复制构造函数不正确 - 可能它没有将堆栈中的最后一个值设置为 null,但我无法弄清楚为什么在放置行时它没有将值设置为 NULL

newPrev->next = NULL;

这是我的代码(仅限班级)

    #include <string>
    using namespace std;
    class Stack 
    {
    protected:
        struct Node 
        {
            string item;
            Node* next; 
        }; // struct Node

    public:
        // constructor of an empty stack
        Stack ()
        {
            head = NULL;
        }

        // copy constructor
        Stack( const Stack & rhs )
        {
            if (rhs.head == NULL) {// check whether original is empty
                head = NULL; 
            }else{
                head = new Node;
                head->item = rhs.head->item;
                Node* newPrev = head;
                // Now, loop through the rest of the stack
                for(Node* cur = rhs.head->next; cur != NULL; cur = cur->next)
                {   
                    newPrev->next = new Node;
                    newPrev = newPrev->next;
                    newPrev->item = cur->item;
                } // end for 

                newPrev->next = NULL;

            } // end else
        }

        // destructor
        ~Stack ()
        {
            delete head; 
        }

        // assignment
        const Stack & operator=( const Stack & rhs )
        {
            return *this;
        }

        // query whether the stack is empty
        bool empty () const
        {
            return false;
        }

        // add an item to the top of the stack
        // this method is complete and correct
        void push (const string & new_item)
        {
            Node* new_node = new Node;
            new_node->item = new_item;
            new_node->next = head;
            head = new_node;
        }

        // remove the item on the top of the stack
        void pop ()
        {
            if (head!=NULL){
                Node *n = head;
                head = head->next;
                delete n;
            } 
        }

        // return the item on the top of the stack, without modifying the stack
        string & top () const
        {
            return head->item;
        }

    private:
        Node* head;
    };

【问题讨论】:

    标签: c++ linked-list segmentation-fault copy-constructor stack


    【解决方案1】:

    您的复制构造函数很好。你没有实现你的bool Stack::empty() 函数。我把它改成了这样:

    // query whether the stack is empty
    bool empty () const
    {
        return head == NULL;
    }
    

    这运行得很好:

    int main()
    {
        Stack s;
        s.push("a");
        s.push("b");
        Stack b(s);
        while(!s.empty())
        {
            cout << s.top() << endl;
            s.pop();
        }
        while(!b.empty())
        {
            cout << b.top() << endl;
            b.pop();
        }
        return 0;
    }
    

    【讨论】:

    • 你说得对,我的 bool 为空是问题,但如果 head 为空,则返回 true,否则返回 false
    猜你喜欢
    • 2013-04-29
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2014-06-17
    • 2016-02-09
    相关资源
    最近更新 更多