#include<iostream>
using namespace std;

class Node 
{	
	public:
		int data;
		Node *preNode;
		Node *postNode;

		//Node(){}
};

int main()
{
	Node testNode;	
	Node testNodeB = testNode;	

	return 0;
}

When I run the code, there occurs an runtime error, The variable "testNode" being used without being initiated. But when write an empty constructor, things turns back to normal.


Working Code:

#include<iostream>
using namespace std;

class Node 
{	
	public:
		int data;
		Node *preNode;
		Node *postNode;

		Node(){}
};

int main()
{
	Node testNode;	
	Node testNodeB = testNode;	

	return 0;
}

This code runs without any problem.

Q: Why is this happening?

Edited 5 Years Ago by iamcreasy: n/a

I only got a warning :D

yes, there is also this warning,

1>e:\vcc_test\vcc_test\main.cpp(17) : warning C4700: uninitialized local variable 'testNode' used

But, program through a run time error.

I am using VS 2008 Express Edition.

I didn't get the runtime error you are mentioning either.I think the problem occurs when the compiler generated copy constructor tries to initialize preNode and postNode since they point to unreferenced memory but it's just a guess.

Sorry i meant the default assignment operator.I assume you want to create a linked list in which case you should declare a default constructor that assigns both member pointers to NULL and you must define the copy constructor and assignment operator to make a deep copy.

Then...

#include<iostream>
using namespace std;

class Node 
{	
	public:
		int data;
};

int main()
{
	Node testNode;	
	Node testNodeB = testNode;	

	return 0;
}

This code isn't working either.unless i do one of the following.

1. Removing all the public & private members :S
2. Entering an empty constructors.

This article has been dead for over six months. Start a new discussion instead.