views:

153

answers:

5

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
+5  A: 

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.

Brian R. Bondy
Good point. +1. :)
Billy ONeal
He doesn't need to define operator= at all. Your definition is the same as the default definition.
Potatoswatter
I assume he posted a simplified example of what he wants to do.
Brian R. Bondy
You forgot to check for self assignment.
Borbus
A: 

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.

Billy ONeal
+1  A: 

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

Borbus
+1  A: 

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;
    }
};
Potatoswatter
A: 

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.

kyoryu