Question: Need help with my code, keep getting this error - Exited with return code - 6 ( SIGABRT ) . free ( ) : double

Need help with my code, keep getting this error- Exited with return code -6(SIGABRT). free(): double free detected in tcache 2.
#include
#include
using namespace std;
class MileageTrackerNode {
public:
double miles;
string date;
MileageTrackerNode* nextNodePtr;
// Constructor
MileageTrackerNode(double miles, string date) : miles(miles), date(date), nextNodePtr(nullptr){}
// InsertAfter function
void InsertAfter(MileageTrackerNode* nodePtr){
nodePtr->nextNodePtr = this->nextNodePtr;
this->nextNodePtr = nodePtr;
}
// PrintNodeData function
void PrintNodeData(){
cout << miles <<","<< date << endl;
}
};
int main(){
int numNodes;
cin >> numNodes;
// Dummy head node (not to be printed)
MileageTrackerNode* headNode = new MileageTrackerNode(0,"");
MileageTrackerNode* lastNode = headNode;
// Reading nodes and building the linked list
for (int i =0; i < numNodes; ++i){
double miles;
string date;
cin >> miles >> date;
MileageTrackerNode* newNode = new MileageTrackerNode(miles, date);
lastNode->InsertAfter(newNode);
lastNode = newNode; // Move to the new last node
}
// Printing nodes (skipping the dummy head node)
MileageTrackerNode* currentNode = headNode->nextNodePtr;
while (currentNode != nullptr){
currentNode->PrintNodeData();
currentNode = currentNode->nextNodePtr;
}
// Cleanup (free allocated memory)
currentNode = headNode;
while (currentNode != nullptr){
MileageTrackerNode* tempNode = currentNode;
currentNode = currentNode->nextNodePtr;
delete tempNode;
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!