开发者

Operator Overloading working but giving stack overflow and crashing in C++

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开发者_如何学JAVA 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


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.


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


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;
    }
};


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜