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:
/****************************************************************************************
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);
}
usingnamespace 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;
}
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?
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'
//******************************************
//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;
}
}