Hello, all! I don't know if 'overridden' is the right word here. In my programming class, I have to create a circular list, such that each node node contains a pointer pointing to the next node and the final node points to the first node. In addition, there is a tail node that points to the last node added (its null before any nodes are added).
I cannot populate my list (called a ring) because every time I call the Ring::Insert(const int& d) function, which inserts a single node, and it gets to the line "RingNode newNode(d);", the new RingNode object overwrites the previous RingNode object that was created when I last called the Ring::Insert(const int& d) function. Obviously, I don't want this because it messes up my list. How do I make it so every time the function creates a brand new RingNode object it doesn't interfere with the previous RingNode objects?
Source code from my header file, just in case:
class RingNode {
public:
RingNode(const int& i=0 ): data(i), next(NULL){}
private:
int data; /* ID of player */
RingNode* next;
friend class Ring;
And here is the function in question
RingNode* Ring::Insert(const int& d){
RingNode newNode(d); //This line overwrites previous RingNode objects
RingNode* refNode = &newNode; //Probably bad form, but that's not my main concern right now
if (tail==null){
tail = refNode;
newNode.next = refNode;
return refNode;
}
newNode.next = (*GetTail()).next;
(*GetTail()).next = refNode;
tail = refNode;
return refNode;
}
So, for example, if I execute the following snippet in my main...
Ring theRing;
theRing.Insert(5);
theRing.Insert(2);
theRing.Insert(7);
If I debug my project I can see that theRing contains only one RingNode, first it's the 5 RingNode, then the 2 RingNode overwrites it, then the 7 RingNode overwrites that. Thanks for reading and double thanks for your replies!
EDIT: I replaced
RingNode newNode(d);
RingNode* refNode = &newNode;
with
RingNode *newNode = new RingNode(d);
tweaked the rest of the code, and it's working properly. Thanks so much for the help, guys! Very informative and best of all I now understand why it was messing up.