运算符重载可以工作,但在 C++ 中会导致堆栈溢出和崩溃;
我编写了这个 Node 类和 = 运算符重载函数,这是我可以让它编译和运行的唯一方法,但它只是溢出并轰炸了我的程序。有人可以修复它吗?我对 C++ 中的重载运算符没有太多经验。我只想将一个 Node 对象设置为等于另一个 Node 对象。提前致谢!
class Node
{
public:
Node();
int y;
Node& operator=(const Node& n);
};
Node::Node(){ y = -1; }
Node& Node::operator=(const Node& n) { return *this = n; }
。
Build issues:
1>c:\users\aaron mckellar\documents\school stuff\cs445\test\test\main.cpp(57) : warning C4717: 'Node::operator=' : recursive on all control paths, function will cause runtime stack overflow
1>Linking...
1>LINK : C:\Users\Aaron McKellar\Documents\School Stuff\CS445\Test\Debug\Test.exe not found or not built by the last incremental link; performing full link
I wrote this Node class and = operator overload function and this is the only way I could get it to compile and run but it just overflows and bomb my program. Can someone please fix it so it works. I don't have a lot of experience with overloading operator in C++. I just want to set a Node object equal to another Node object. Thanks in advance!
class Node
{
public:
Node();
int y;
Node& operator=(const Node& n);
};
Node::Node(){ y = -1; }
Node& Node::operator=(const Node& n) { return *this = n; }
.
Build issues:
1>c:\users\aaron mckellar\documents\school stuff\cs445\test\test\main.cpp(57) : warning C4717: 'Node::operator=' : recursive on all control paths, function will cause runtime stack overflow
1>Linking...
1>LINK : C:\Users\Aaron McKellar\Documents\School Stuff\CS445\Test\Debug\Test.exe not found or not built by the last incremental link; performing full link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的
operator=
中,您需要将成员变量设置为等于传入的 Node 值。您用英语所做的相应示例:狗的定义是狗。狗的定义不是说它是狼的驯化形式,而是食肉目犬科的成员。
in your
operator=
you need to do the work of setting the member variables equal to the passed in Node's value.A corresponding example of what you did in English: The definition of a dog is a dog. Instead of saying the definition of a dog is a domesticated form of the wolf, a member of the Canidae family of the order Carnivora.
你有一个无限递归,因为 *this = n 正在调用 Node::operator=() (因为这是一个 Node)。赋值运算符的通常行为类似于复制构造函数,但您必须检查自赋值。请参阅:http://www.parashift.com/c++-faq-精简版/赋值运算符.html
You have an infinite recursion because *this = n is calling Node::operator=() (because this is a Node). The usual behaviour of the assignment operator is like a copy constructor, but you must check for self assignment. See: http://www.parashift.com/c++-faq-lite/assignment-operators.html
只需删除有问题的功能即可。 C++ 为您定义了一个默认的
operator=
。该函数在任何意义上都“存在”:您可以获得指向它的指针等。但是,如果您添加重载,则默认值完全消失,并且在其中使用=
会导致递归。如果您确实需要向
operator=
添加其他功能,则您还必须自己实现默认功能,即y = ny;
。您的重载会杀死默认的=
,因此*this=n
除了调用自身之外没有任何事可做。如果您有很多数据成员,一种替代方法是声明一个类以仅使用其默认的
operator=
:Simply remove the offending function. C++ defines a default
operator=
for you. The function is "there" in every sense: you can get a pointer to it, etc. However, if you add an overload, the default goes away completely, and using=
within it causes recursion.If you actually have additional functionality to add to
operator=
, you will have to implement the default functionality as well, yourself, iey = n.y;
. Your overload kills the default=
so*this=n
has nothing to do except call itself.If you have a lot of data members, one alternative would be to declare a class just to use its default
operator=
:您的问题是
Node::operator=(const Node& n)
包含return *this = n;
。return *this = n;
调用operator=
因为您在类的一个实例和类的另一个实例上使用 = 。Your problem is that
Node::operator=(const Node& n)
containsreturn *this = n;
.return *this = n;
callsoperator=
because you are using = on an instance of your class and another instance of your class.您的重载运算符是如下所示的代码所调用的:
由于“*nodeptr”相当于“node”,因此您创建了一个调用自身的运算符。
Your overloaded operator is what is called for code that looks like this:
Since '*nodeptr' is equivalent to 'node', you have created an operator which calls itself.