Difficulty with uninitialized variable (C4703)

Im getting an error on line 142 saying my pointer is uninitialized but i have initialized it on line 114. This is the error:

Error 1 error C4703: potentially uninitialized local pointer variable 'previousNode' used

Can anyone tell me what i am doing wrong? and also if my print function is correct. I was unable to compile to see if it is running correctly. Thanks for any help! Here is my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  /****************************************************************************************


Chapter 17, Programming Challenge 2: List Print

Written by: 
Date: 8/13/15

****************************************************************************************/

// ************************************************************************************** 
// INCLUDES
// ************************************************************************************** 

// system
//
#include <iostream>
#include "intList.h"

// ************************************************************************************** 
// FUNCTIONS
// ************************************************************************************** 

//***********************************************************
// appendNode to add a node                                 *
//***********************************************************

void IntList::appendNode(int num)
{
	ListNode *newNode; //Points to the new node
	ListNode *nodePtr; //Moves through the list

	//Allocate new node and store number in it
	newNode = new ListNode;
	newNode->value = num;
	newNode->next = nullptr;

	//If no nodes then newNode is the first node
	if (!head)
		head = newNode;
	//If notinsert newNode at end
	else
	{
		//nodePtr points at head
		nodePtr = head; 
		
		//Find the last node
		while (nodePtr->next)
			nodePtr = nodePtr->next;
		
		//Insert newNode as last node
		nodePtr->next = newNode; 
	}
}


//******************************************************
//insertNode if node needs to be placed in middle      *
//******************************************************

void IntList::insertNode(int num)
{
	ListNode *newNode; //new node
	ListNode *nodePtr; //Moves through the list
	ListNode *previousNode = nullptr; //Points to previous node

	//Allocate new node and store number in it
	newNode = new ListNode;
	newNode->value = num;

	//If no nodes then newNode is the first node
	if (!head)
		head = newNode;
	//If notinsert newNode at end
	else
	{
		//nodePtr points at head
		nodePtr = head;

		//make previosNode = null
		previousNode = nullptr;

		//skip nodes with vale < number
		while (nodePtr != nullptr && nodePtr->value < num)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		//If new node is 1st, place before all other nodes
		if (previousNode == nullptr)
		{
			head = newNode;
			newNode->next = nodePtr;
		}

		//Or else insert after previous node
		else
		{
			previousNode->next = newNode;
			newNode->next = nodePtr;
		}
	}
}


//*******************************************************
//deleteNode to remove the nodes from memory            *
//*******************************************************

void IntList::deleteNode(int num)
{
	ListNode *nodePtr; //Moves through List
	ListNode *previousNode; //Points to previous node

	//If there is no list no action is needed
	if (!head)
		return;
	//determines if first node is the one to be deleted
	if (head->value == num)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}
	else
	{
		//make nodePtr point to head
		nodePtr = head;

		//Skip nodes that dont have the same value as num
		while (nodePtr != nullptr && nodePtr->value != num)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		//If nodePtr isnt last in list connect previous node to the
		//node after nodePtr and then delete nodePtr
		if (nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}


//*************************************************
//Print to display the data in the nodes          *
//*************************************************
void IntList::print()
{
	if (head == NULL)
	{
		std::cout << " This list is empty! "; 
	}

	ListNode *nodePtr = head; 
	while (nodePtr != NULL)
	{
		std::cout << nodePtr->value;
		if (nodePtr->next != NULL)
			std::cout << "==>";
		nodePtr = nodePtr->next;
	}
	delete (nodePtr);
}

using namespace std;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// main
//
//
int main()
{
	// local variables
	//
	IntList list;

	// initialization
	//
	list.appendNode(2);  // Append 2 to the list
	list.appendNode(4);  // Append 4 to the list
	list.appendNode(6);  // Append 6 to the list

	// Display the nodes.
	//
	cout << "Here are the initial values:\n";
	list.print();
	cout << endl;

	// Insert the value 5 into the list.
	//
	cout << "Now inserting the value 5.\n";
	list.insertNode(5);

	// Display the nodes now.
	//
	cout << "Here are the nodes now.\n";
	list.print();
	cout << endl;

	// Delete the node holding 6.
	//
	cout << "Now deleting the last node.\n";
	list.deleteNode(6);

	// Display the nodes.
	//
	cout << "Here are the nodes left.\n";
	list.print();

	return 0;
}








and my .h file



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  /****************************************************************************************


Chapter 17, Programming Challenge 2: List Print

Written by: 
Date: 8/13/15

****************************************************************************************/

#ifndef INTLIST_H
#define INTLIST_H

// ************************************************************************************** 
// CLASS DECLARATION
// ************************************************************************************** 

class IntList
{
private:
	struct ListNode
	{
		int value;
		struct ListNode *next;
	};

	ListNode *head;   // List head pointer

public:
	IntList()   // Constructor
	{
		head = nullptr;
	}
	~IntList(); // Destructor
	void appendNode(int);
	void insertNode(int);
	void deleteNode(int);
	void print();
};

#endif. 
Last edited on
but i have initialized it on line 114

No, you declared the pointer at line 114, but you did not initialize it.
The pointer contains garbage.

You do set previousNode at line 134, but there is no guarantee that the body of the while loop will execute.

Not sure why you're even bothering with previousNode at line 134 and 142.
You never reference it.
Last edited on
Ahhh yes i see. Since its just pointing to whatever was stored in that address.

i need the previous node so i can connect the node after deleting one. For example if i have node 123 and i want to delete node 2 i want to then connect the previous node (1) to node 3 to complete the linked list. Im following the example in my c++ book and they did not reference anything either.

im using the book C++ from control structures through objects 8th edition by Tony Gaddis if that helps.

I tried to initialize the pointers to nullptr but i got more compiler errors. I get:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall IntList::~IntList(void)" (??1IntList@@QAE@XZ) referenced in function _main

and

Error 2 error LNK1120: 1 unresolved externals

Im tried to google the error but it appears there are many things that can cause this error. Can you help me identify it?
Last edited on
Looks like your class either declared but not defined destructor or inherited from class with pure virtual destructor.

About uninitialized variable error: just initialize the variable. Error message is bogus however: at no code path that pointer can be used uninitialized.
I figured it out. I initialized it to nullptr and my LNK errors were because i did not use themake a function for the destructor. I am now getting a few new errors that i need some help with. In my destructor function i am getting these two errors on line 178 (11) and 183(16):

Error 1 error C2440: '=' : cannot convert from 'IntList::ListNode *' to 'IntList *'

and

Error 2 error C2039: 'next' : is not a member of 'IntList'

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//******************************************
//Destructor to delete all Nodes           *
//******************************************

IntList::~IntList()
{
	IntList *nodePtr; //move through list
	IntList * nextNode; //Points to next node

	//make nodeptr point to head
	nodePtr = head; 

	while (nodePtr != nullptr)
	{
		//save pointer to next node
		nextNode = nodePtr->next;

		//delete node
		delete nodePtr;

		nodePtr = nextNode;
	}
}
Last edited on
IntList *nodePtr Are you sure that you want IntList here?
ohhhhhhhh I needed ListNode. Thanks you MiiNiPaa and you too abstraction Anon. Thanks for taking time to help out a novice programmer :)

THANKS SOOOOOOOOO MUCH!!! I just ran the program and it works perfectly!
Last edited on
Topic archived. No new replies allowed.