Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
class Node {
public:
	int value;
	Node* next=NULL;
	

	Node() {
		next = new Node();
		next->next = NULL;
	}
};


What I have tried:

Everytime i make a new object of this class. I get an exception Stack overflow.
Posted
Updated 17-Sep-18 5:19am

1 solution

Simple: the first thing your Node constructor does is try to construct a new Node instance. Which calls the Node constructor for that object instance, and the constructor tries to create another new Node instance. So the constructor is called for that one, which ... and you run out of stack space.

Solution: Don't create a new Node in the Node constructor!
 
Share this answer
 
Comments
CPallini 17-Sep-18 11:21am    
5.
KarstenK 18-Sep-18 7:04am    
It is a recursive call of the constructor. :-O

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900