Aar*_*lar 2 c++ stack-overflow operator-overloading
我写了这个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; }
Run Code Online (Sandbox Code Playgroud)
.
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
Run Code Online (Sandbox Code Playgroud)
在你的operator=
,你需要做的设置成员变量等于在节点的值传递的工作.
Node& Node::operator=(const Node& n)
{
y = n.y;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
你用英语做的相应例子:狗的定义是狗.而不是说狗的定义是狼的驯化形式,是食肉目的Canidae家族的成员.