I'll try to keep this brief. I have multiple classes and some classes have objects from another class as a private member. I created "set" mutator functions to instantiate class objects of the private members. I am getting exception errors when it tries to create the objects. My header files are:
Flight.h, Reservation.h, Passenger.h
The main.cpp looks messy right now because I've been playing around with many different ways to make this work and check my work, so please excuse how it looks for now. This is my first time posting a question in this forum, so I hope I did this right.
My main.cpp file is:
.
.
.
Flight newFlight(cityFrom, cityTo, flightNum, departT, arrivT, planeT, planeP);
Flight aFlight;
//Reservation newRes(fname, lname, newFlight); //this way did not work
Reservation *newRes, aRes;
newRes->setFirstName(fname);
newRes->setLastName(lname);
//in debugger, this is what's blowing up the program
newRes->setFlightRes(newFlight);
//Passenger newPass(fname, lname, newRes); //this way did not work
Passenger newPass;
newPass.setFirstName(fname);
newPass.setLastName(lname);
//I'm assuming this will blow up as well
newPass.setResPass(*newRes);
//define the string variables as a way of verifying the data in the passenger //object was created correctly
fname = newPass.getFirstName();
lname = newPass.getLastName();
aRes = newPass.getResPass();
aFlight = aRes.getFlightRes();
cityFrom = aFlight.getCityFrom();
cityTo = aFlight.getCityTo();
departT = aFlight.getDepartTime();
arrivT = aFlight.getArrivalTime();
flightNum = aFlight.getFlightNum();
planeT = aFlight.getFlightType();
planeP = aFlight.getFlightPoints();
Once again, please excuse the sloppiness. Just brainstorming right now.
Any help with the syntax to get it working would be greatly appreciated.
The problem is that the argument to your mutator function is a temporary. You assign the address of the temporary to the flightRes member. When the function terminates, the reservation data is cleaned up -- but your class still thinks it has a valid value at some random spot on the stack. So when you try to access it, you get data corruption and/or access violation.
Make it Flight flightRes; and life will work fine.
There might be other issues, but you did not put nicely-formated code in [code] blocks, so I'm not going to strain my eyeballs any further.
Thank you Duoas! So far it's working but I have more to go and not much time left to finish it. Not that I procrastinated, just family issues destroyed my concentration and time availability so I'm going to submit what I have working on Tuesday. Hopefully you will still be available if I have more questions, and I assure you I will block the code better next time. This was my first attempt at using this forum, so I wasn't sure how it should look.