C++ - 链表堆栈运算符重载功能
我目前正在开发一个实现链表的堆栈。我在重载“=”运算符时遇到问题。我非常不知道该怎么办。如果有人能给我指出一个好的方向,那就太好了。
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在将数据推送到当前堆栈之前,您需要清空当前堆栈。您应该添加一个removeAll函数,并在分配的顶部调用它(在检查自分配之后,这也是一个好主意)。否则,它看起来是正确的。因此,最终结果将是:
这是一个示例 removeAll 函数:
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:
Here is a sample removeAll function:
不要为您的类手动实现复制赋值运算符,而是使用 copy-and-交换习语。
一旦您为您的类实现了
swap()
函数(我上面链接的文章提供了如何执行此操作的精彩描述),operator=
重载变得简短而简单: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), theoperator=
overload becomes short and simple: