如何通过friend的功能将对象(类型a)传递给另一个对象(类型b)的私有对象(类型a)

时间:2016-05-17 03:18:47

标签: c++ arrays class friend

#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;
}

我需要通过函数addPassenger将Passenger Class的数组对象传递到Flight Class的对象里面的Passenger Class的私有数组对象。 一切都编译得很好,但是我无法获得printList(对象Flight),它只是跳出来并终止控制台。

很抱歉,由于项目的要求,它非常复杂。 希望有一些帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

问题1

主要问题是在行

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

问题的根源是pass的成员变量Flight没有正确初始化,然后你继续使用f.pass,好像它是一个有效的指针。

问题2

函数addPassengerFlight个对象作为输入。当您调用该函数时,该函数将获取原始Flight对象的副本。对Flight中的addPassenger对象所做的任何更改都是针对本地副本,而不是main中的对象。

通过以下更改,我能够毫无问题地运行您的程序。

  1. Flight变量的成员更改为:

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

    删除了成员变量pass

  2. addPassenger的签名更改为。

    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);
       }
    }
    
  3. Flight::printList的实施更改为:

    void printList(){
       for(auto& p : passengerList ){
          cout << p.name << endl;
       }
    }
    
  4. pasmain的类型更改为:

    std::vector<Passenger> pas;
    
  5. 需要更改其他内容以应对这些更改。这是完整的程序:

    #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;
    }
    

    http://ideone.com/Chttoe处查看它。