Just need the correct syntax please (or a better way would be good too)

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

class Flight {
private:

string cityFrom;
string cityTo;
string departTime;
string arrivalTime;
string flightNum;
string flightType;
string flightPoints;

public:

//default constructor
Flight()
{
cityFrom = ""; cityTo = "";
departTime = ""; arrivalTime = "";
flightNum = ""; flightType = ""; flightPoints = "";
}

//constructor
Flight(string cFrom, string cTo, string dTime, string aTime, string fNum, string fType, string fPts)
{
cityFrom = cFrom; cityTo = cTo;
departTime = dTime; arrivalTime = aTime;
flightNum = fNum; flightType = fType; flightPoints = fPts;
}


class Reservation {

private:

string firstName;
string lastName;
Flight *flightRes;

public:

//default constructor
Reservation()
{
firstName = ""; lastName = ""; flightRes = NULL;
}

//constructor
Reservation(string first, string last)
{
firstName = first; lastName = last; flightRes = NULL;
}

//copy constructor
Reservation(const Reservation &resrv)
{
firstName = resrv.firstName;
lastName = resrv.lastName;
flightRes = new Flight(*resrv.flightRes);
}

//Mutator functions

//set firstName
void setFirstName(string fn) { firstName = fn; }
//set lastName
void setLastName(string ln) { lastName = ln; }
//set flightRes
void setFlightRes(Flight fs) { *flightRes = fs; }



class Passenger {

private:

string firstName;
string lastName;
Reservation *resPass;

public:

//default constructor
Passenger()
{
firstName = ""; lastName = ""; resPass = NULL;
}

//constructor
Passenger(string first, string last)
{
firstName = first; lastName = last; resPass = NULL;
}

//copy constructor
Passenger(const Passenger &passg)
{
firstName = passg.firstName;
lastName = passg.lastName;
resPass = new Reservation(*passg.resPass);
}

//Mutator functions
//set firstName
void setFirstName(string fn) { firstName = fn; }
//set lastName
void setLastName(string ln) { lastName = ln; }
//set resPass
void setResPass(Reservation rp) { *resPass = rp; }



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.
Last edited on
Why is the flight data a pointer?

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.

Hope this helps.
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.
You're doing great. I understand how family issues can get in the way of schooling. And I'm glad to help whenever I can.
Topic archived. No new replies allowed.