This code is to simulate a train where you start with an engine and you can add train cars, detach train cars and move between train cars. I have no problem with that but I have a problem with deleting all my new boxes that I have created while adding the trains at the end of the program. I want all my new train cars to remain until the user quits. At that point, I want to delete all the new boxes. How do I do that? I tried adding a destructor to my class with "delete next;" and "delete previous;" but the program had a run time error. I am new to pointers and dynamic memory and I want to know how to do that. Thanks very much!
Most likely you weren't deleting safely and tried to access/delete a dangling pointer. Your train acts like a doubly linked list, so you should consider using a temporary variable to hold the next value, while deleting the previous one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
~train()
{
if(engine!=nullptr)
{
auto current=engine; //use this to iterate through your cars
auto temp=current; // use this to store next value
while(current->next!=nullptr)
{
temp=current->next; // before you delete your value, store the next car, otherwise you won't be able to access it
delete current; // delete car
current=temp; //move forwards to the next car
}
if(temp) delete temp;
temp=nullptr;
}
};
Without looking at your class and problem in minute detail I get the impression your data model would be better off if you differentiated between a train object and a list of trains and make them two separate classes.
Doing it this way makes it a lot clearer what adding and deleting a train means. deleting a train via a destructor is a lot different from removing a train from a list of linked trains while maintaining its existence for later or other use.
I kind of made it unclear as to what the instructions are to do so I will put them below. For our assignment we were given a template that the course instructor coded and I was able to implement the adding of a train. This is what I'm having trouble with though:
1 2 3 4
Add functionality to detach the
previous train (the one behind the current train), if it exists. This time you need to ensure there are
no memory leaks and all “new”s are deleted. Any cars on the train that are left once the user quits
the loop should be deleted at the end of main().
We were given a template that I had to modify, I was able to add the "add" functionality but with detach there is certainly a memory leak. I don't know how I would delete the 'new' that I have. We need to "ensure there are no memory leaks and all “new”s are deleted."
You need to be clear on what the purpose of your add method is, and why you need to have a new Train x in it when the constructor can do that.
A linkage of train objects can be achieved by each train having pointers to the next and previous trains as you have done. Those pointers would be to train objects created by the constructor outside the class rather than inside the class which creates something of a nightmare.
The thing is, with this assignment, the first part in which you create the "add" function you are not allowed to change main(). The main we are given is
#include <iostream>
usingnamespace std;
int main()
{
train engine = train("Engine");
train* current = &engine;
string choice;
do
{
if(current -> hasNext())
{
cout << "Next train: " << current -> nextTrain() -> getName() << endl;
}
cout << "Current train: " << current -> getName() << endl;
if(current -> hasPrevious())
{
cout << "Previous train: " << current -> previousTrain() -> getName() << endl;
}
cout << "Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?\n";
getline(cin,choice);
if(tolower(choice[0]) == 'n' && current -> hasNext())
{
current = current -> nextTrain();
}
elseif(tolower(choice[0]) == 'p' && current -> hasPrevious())
{
current = current -> previousTrain();
}
elseif(tolower(choice[0]) == 'a')
{
cout << "Which train is this?\n";
string name;
getline(cin, name);
current->add(name);
}
}while(tolower(choice[0]) != 'q');
}
Everything we do has to make this main() work. The detach part which is what I'm having trouble with says nothing about keeping main the same, so if some of this looks coded weird, most likely it was because I was trying to get something to work with main.
If you can't modify main() in any way then someone might come along and have a neat answer to the way you are creating a train object in your add method instead of the constructor.