C++ - 链表堆栈运算符重载功能

发布于 2024-09-30 01:10:37 字数 621 浏览 4 评论 0原文

我目前正在开发一个实现链表的堆栈。我在重载“=”运算符时遇到问题。我非常不知道该怎么办。如果有人能给我指出一个好的方向,那就太好了。

//operator overload
template <class S>
const Stack<S>::operator=( const Stack& s )
{

    if (s.isEmpty())
        theFront = theTop = 0
    else
    {
        NodePointer temp = q->theFront;

        while(temp != 0)
        {
            push(temp->data);
            temp = temp->next;
        }
    }

    return *this;
}

我也收到此错误: 堆栈,std::分配器> >::Node::Node(std::basic_string, std::allocator >)' 引用自 C:\USERS\JOHNNY\DESKTOP\STACK\INFIX_TO_RPN.OBJ

这可以通过我的运算符重载函数来修复吗?

I am currently working on a stack that implements linked list. I am having a problem when it comes to overloading the "=" operator. I am very clueless as to what to do. If anyone could point me in a good direction that would be awesome.

//operator overload
template <class S>
const Stack<S>::operator=( const Stack& s )
{

    if (s.isEmpty())
        theFront = theTop = 0
    else
    {
        NodePointer temp = q->theFront;

        while(temp != 0)
        {
            push(temp->data);
            temp = temp->next;
        }
    }

    return *this;
}

I am also getting this error :
Stack, std::allocator > >::Node::Node(std::basic_string, std::allocator >)' referenced from C:\USERS\JOHNNY\DESKTOP\STACK\INFIX_TO_RPN.OBJ

Can this be fixed by my operator overload function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-10-07 01:10:37

在将数据推送到当前堆栈之前,您需要清空当前堆栈。您应该添加一个removeAll函数,并在分配的顶部调用它(在检查自分配之后,这也是一个好主意)。否则,它看起来是正确的。因此,最终结果将是:

//operator overload 
template <class S> 
const Stack<S>::operator=( const Stack& s ) 
{ 
    // Check for self assignment
    if (&s==this)
        return *this;

    // Clear the current stack
    removeAll();

    // Copy all data from stack s
    if (!s.isEmpty())
    { 
        NodePointer temp = q->theFront; 

        while(temp != 0) 
        { 
            push(temp->data); 
            temp = temp->next; 
        } 
    } 

    return *this; 
} 

这是一个示例 removeAll 函数:

template <class S> 
void Stack<S>::removeAll()    
{ 
    while (s.theFront)
    {
        NodePointer p = s.theFront;

        s.theFront = s.theFront->next;
        delete p;
    }

    s.theTop = s.theFront;
}

You need to empty the current stack, before pushing data on it. You should add a removeAll function, and call it at the top of the assignment (after a check for self assignment, which is also a good idea). Otherwise, it looks correct. So, the end result would be:

//operator overload 
template <class S> 
const Stack<S>::operator=( const Stack& s ) 
{ 
    // Check for self assignment
    if (&s==this)
        return *this;

    // Clear the current stack
    removeAll();

    // Copy all data from stack s
    if (!s.isEmpty())
    { 
        NodePointer temp = q->theFront; 

        while(temp != 0) 
        { 
            push(temp->data); 
            temp = temp->next; 
        } 
    } 

    return *this; 
} 

Here is a sample removeAll function:

template <class S> 
void Stack<S>::removeAll()    
{ 
    while (s.theFront)
    {
        NodePointer p = s.theFront;

        s.theFront = s.theFront->next;
        delete p;
    }

    s.theTop = s.theFront;
}
带刺的爱情 2024-10-07 01:10:37

不要为您的类手动实现复制赋值运算符,而是使用 copy-and-交换习语

一旦您为您的类实现了 swap() 函数(我上面链接的文章提供了如何执行此操作的精彩描述),operator= 重载变得简短而简单:

Stack& operator=(Stack rhs)
{
    swap(rhs);
    return *this;
}

Instead of manually implementing the copy assignment operator for your class, use the copy-and-swap idiom.

Once you've implemented a swap() function for your class (the article to which I linked above provides an excellent description of how to do this), the operator= overload becomes short and simple:

Stack& operator=(Stack rhs)
{
    swap(rhs);
    return *this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文