-1
#include <iostream>
#include <vector>
using namespace std;

class Flight;

class Time {
   private :
      int hour;
      int minute;
   public :
      Time(int hour,int minute){

         this->hour = hour;
         this->minute = minute;

      };
      int getHour(){
         return hour;
      }
      int getMinute(){
         return minute;
      }
};

class Passenger{
   private:
      string name;
      int age;
   public :
      Passenger(string name , int age){

         this->name = name;
         this->age = age;

      }
      void printDetails(){

         cout << "Name: " << name << "\t";   
         cout << "Age: " << age <<"\t";              
      }
      friend void addPassenger(Passenger *p,int num,Flight f);
      friend Flight;
};

class Flight {

   private :
      string id;
      string destination;
      Time *depart;
      Time *arrival;
      vector<Passenger> passengerList;
      Passenger *pass;

   public :
      Flight(string id, string destination, Time *t, Time *a){

         this->id = id;
         this->destination = destination;
         depart = t;
         arrival = a;
         id = 3;
      };

      void printInfo(){
         cout<< "Flight Number : " << id << endl;
         cout<< "Destination : " << destination << endl;
         cout<< "Desparture : " << depart->getHour() << ":" << depart->getMinute()<< endl;
         cout<< "Arrival : " << arrival->getHour() << ":" << arrival->getMinute() << endl;
      }
      void printList(){

         cout << pass->name[0];

      }
      friend class Passenger;
      friend void addPassenger(Passenger *p,int num,Flight f);
};

void addPassenger(Passenger *p,int num,Flight f){

   //  for(int i=0;i<num;i++){

   //  f.passengerList.push_back(p[i]);
   f.pass->name = p->name;
   //  }

}

int main(){

   int num_passenger;
   int temp_age;
   string temp_name;

   vector<int> passenger_age;
   vector<string> passenger_name;

   Time t(2,4);
   Time t2(2,3);
   Flight ff("3","4",&t,&t2);

   cout<< "Enter the number of passenger" << endl;
   cin>> num_passenger;
   cout<< endl;

   Passenger *pas[num_passenger];

   for(int i=0;i < num_passenger; i++){

      cout<< "Enter the name of adult "<< i+1 << endl;
      cin>> temp_name;
      passenger_name.push_back(temp_name);
      cout<< "Enter the age of adult "<< i+1 << endl;
      cin>> temp_age;
      passenger_age.push_back(temp_age);


   }

   for(int p=0; p < num_passenger; p++){

      pas[p] =  new Passenger(passenger_name[p],passenger_age[p]);

   }
   addPassenger(*pas,2,ff);
   ff.printList();

   return 0;
}

I need to pass array object of Passenger Class into private array object of Passenger Class that inside object of Flight Class through function addPassenger. Everything compile fine, but I can't get printList(object Flight) work, it simply jumps out and terminates the console.

Sorry it quite complicated due to requirement of project. Hope to have some helps, Thanks.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
WJ LIM
  • 31
  • 9
  • `printList()` - as posted - tries to dereference `pass`, but none of your code ever initialises that pointer. Why do you need both `Passenger* pass` and `vector`? The code's so incomplete it barely makes sense to talk about what's broken. – Tony Delroy May 17 '16 at 03:26
  • First i try on vector, but it fails too, then i try pointer. Im not pretty familiar with c++, have no idea how to solve the problem. – WJ LIM May 17 '16 at 03:32
  • I suggest you don't use any pointers anywhere (i.e. in `Flight` have `Time depart; Time arrival;`, `Flight(string id, string destination, Time t, Time a)`, `void addPassenger(Passenger p,int num,Flight f)` etc.). Get rid of `Passenger *pass;`. Google `vector::push_back`. Write some smaller programs to teach yourself what you need to know in stages. – Tony Delroy May 17 '16 at 03:53
  • You are right, thank you – WJ LIM May 17 '16 at 04:30

1 Answers1

1

Problem 1

The main problem is at line

f.pass->name = p->name;

The source of the problem is that the member variable pass of Flight is not initialized properly and then you go on to use f.pass as though it is a valid pointer.

Problem 2

The function addPassenger takes a Flight object as input. When you call the function, the function gets a copy of the original Flight object. Any changes made to the Flight object in addPassenger are to the local copy, not the object from main.


With the following changes, I was able to run your program without any problem.

  1. Changed the member of variable of Flight to:

    string id;
    string destination;
    Time *depart;
    Time *arrival;
    vector<Passenger> passengerList;
    

    Removed the member varible pass.

  2. Changed the signature of addPassenger to.

    void addPassenger(std::vector<Passenger> const& plist, Flight& f);
    

    and changed its implementation to

    void addPassenger(std::vector<Passenger> const& plist, Flight& f)
    {
       for(auto& p : plist ){
          f.passengerList.push_back(p);
       }
    }
    
  3. Changed the implementation of Flight::printList to:

    void printList(){
       for(auto& p : passengerList ){
          cout << p.name << endl;
       }
    }
    
  4. Changed the type of pas in main to:

    std::vector<Passenger> pas;
    

Other things needed to be changed to account for these changes. Here's the complete program:

#include <iostream>
#include <vector>
using namespace std;

class Flight;

class Time {
   private :
      int hour;
      int minute;
   public :
      Time(int hour,int minute){

         this->hour = hour;
         this->minute = minute;

      };
      int getHour(){
         return hour;
      }
      int getMinute(){
         return minute;
      }
};

class Passenger{
   private:
      string name;
      int age;
   public :
      Passenger(string name , int age){

         this->name = name;
         this->age = age;

      }
      void printDetails(){

         cout << "Name: " << name << "\t";   
         cout << "Age: " << age <<"\t";              
      }
      friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
      friend Flight;
};

class Flight {

   private :
      string id;
      string destination;
      Time *depart;
      Time *arrival;
      vector<Passenger> passengerList;

   public :
      Flight(string id, string destination, Time *t, Time *a){

         this->id = id;
         this->destination = destination;
         depart = t;
         arrival = a;
         id = 3;
      };

      void printInfo(){
         cout<< "Flight Number : " << id << endl;
         cout<< "Destination : " << destination << endl;
         cout<< "Desparture : " << depart->getHour() << ":" << depart->getMinute()<< endl;
         cout<< "Arrival : " << arrival->getHour() << ":" << arrival->getMinute() << endl;
      }

      void printList(){
         for(auto& p : passengerList ){
            cout << p.name << endl;
         }
      }

      friend class Passenger;
      friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
};

void addPassenger(std::vector<Passenger> const& plist, Flight& f)
{
   for(auto& p : plist ){
      f.passengerList.push_back(p);
   }
}

int main(){

   int num_passenger;
   int temp_age;
   string temp_name;

   vector<int> passenger_age;
   vector<string> passenger_name;

   Time t(2,4);
   Time t2(2,3);
   Flight ff("3","4",&t,&t2);

   cout<< "Enter the number of passenger" << endl;
   cin>> num_passenger;
   cout<< endl;

   std::vector<Passenger> pas;

   for(int i=0;i < num_passenger; i++){

      cout<< "Enter the name of adult "<< i+1 << endl;
      cin>> temp_name;
      passenger_name.push_back(temp_name);
      cout<< "Enter the age of adult "<< i+1 << endl;
      cin>> temp_age;
      passenger_age.push_back(temp_age);


   }

   for(int p=0; p < num_passenger; p++){
      pas.push_back(Passenger(passenger_name[p],passenger_age[p]));
   }

   addPassenger(pas, ff);
   ff.printList();

   return 0;
}

See it working at http://ideone.com/Chttoe.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thanks for your response, but how could i initialize a pointer? – WJ LIM May 17 '16 at 03:30
  • @WJLIM, it's not clear from your post how you want to capture the relationships between `Flight` and `Passenger`. You have two members - `passengerList` and `pass`. I suggest taking some time to think through the relationships. Maybe it will bring some clarity to your code also. – R Sahu May 17 '16 at 03:34
  • @R Sahu I just have to pass the Passenger data into Flight object. The reason that i have two members passengerList and pass is simply because i tried both of them. Then i use only pass in this code. – WJ LIM May 17 '16 at 03:38
  • @WJLIM, that's not what I am talking about. A `Flight` has a list of passengers. That's easy. The question is: Does a `Flight` object own the `Passenger`s? Do these `Passenger`s need to be a list of objects or list of pointers? – R Sahu May 17 '16 at 03:39
  • Yes, i need Passengers to be list of objects in Flight. At first, i tried array, with some modification Passenger pass[]---- void addPassenger(Passenger p[],int num,Flight f)---- f.pass[i]= p[i]; – WJ LIM May 17 '16 at 03:47
  • Thanks for your help, you are so nice. But my c++ is 98 mode. – WJ LIM May 17 '16 at 04:21
  • @WJLIM, in that case, change the `for` loops to use regular iterators. – R Sahu May 17 '16 at 04:22
  • I changed to normal for(int i;i < list.size() ; i++) everthing works fine, but it seems like nothing print out, i will check it again. Thank you very much – WJ LIM May 17 '16 at 04:24
  • Ok, Everthing works fine. I stuck for whole day dy. Thanks once again. – WJ LIM May 17 '16 at 04:27
  • @WJLIM, Excellent. Happy programming. – R Sahu May 17 '16 at 04:47