运算符重载可以工作,但在 C++ 中会导致堆栈溢出和崩溃;

发布于 2024-08-22 19:39:34 字数 769 浏览 9 评论 0原文

我编写了这个 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 技术交流群。

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

发布评论

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

评论(5

听你说爱我 2024-08-29 19:39:34

在您的 operator= 中,您需要将成员变量设置为等于传入的 Node 值。

Node& Node::operator=(const Node& n) 
{ 
    y = n.y;
    return *this; 
}

您用英语所做的相应示例:狗的定义是狗。狗的定义不是说它是狼的驯化形式,而是食肉目犬科的成员。

in your operator= you need to do the work of setting the member variables equal to the passed in Node's value.

Node& Node::operator=(const Node& n) 
{ 
    y = n.y;
    return *this; 
}

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.

挽清梦 2024-08-29 19:39:34

你有一个无限递归,因为 *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

卖梦商人 2024-08-29 19:39:34

只需删除有问题的功能即可。 C++ 为您定义了一个默认的operator=。该函数在任何意义上都“存在”:您可以获得指向它的指针等。但是,如果您添加重载,则默认值完全消失,并且在其中使用 = 会导致递归。

如果您确实需要向 operator= 添加其他功能,则您还必须自己实现默认功能,即 y = ny;。您的重载会杀死默认的 =,因此 *this=n 除了调用自身之外没有任何事可做。

如果您有很多数据成员,一种替代方法是声明一个类以仅使用其默认的 operator=

struct Node_data { // "POD" struct for just data, no methods
    int q,r,s,t,u,v,w,x,y,z; // rough example ;v)
};

class Node : public Node_data {
    …
    Node & Node::operator=( const Node &n ) {
        Node_data::operator=( n ); // compiler-generated func
        cout << "operator= called" << endl;
    }
};

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, ie y = 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=:

struct Node_data { // "POD" struct for just data, no methods
    int q,r,s,t,u,v,w,x,y,z; // rough example ;v)
};

class Node : public Node_data {
    …
    Node & Node::operator=( const Node &n ) {
        Node_data::operator=( n ); // compiler-generated func
        cout << "operator= called" << endl;
    }
};
旧情勿念 2024-08-29 19:39:34

您的问题是 Node::operator=(const Node& n) 包含 return *this = n;return *this = n; 调用 operator= 因为您在类的一个实例和类的另一个实例上使用 = 。

Your problem is that Node::operator=(const Node& n) contains return *this = n;. return *this = n; calls operator= because you are using = on an instance of your class and another instance of your class.

咋地 2024-08-29 19:39:34

您的重载运算符是如下所示的代码所调用的:

node = someothernode;

由于“*nodeptr”相当于“node”,因此您创建了一个调用自身的运算符。

Your overloaded operator is what is called for code that looks like this:

node = someothernode;

Since '*nodeptr' is equivalent to 'node', you have created an operator which calls itself.

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