C++ help needed with calling a function from another class...

hi i'm trying to call a function "issueTicket" from class "ticket", in the the function "reserveCabin" in another class called "cruise". and am having same problem calling function "calTicket" from class "booking", in the function "issueTicket" from class "ticket".

i used "friend class" but i think i'm still not doing it properly.. What am i doing wrong? or not doing?

Below is the code for class cruise, function reserveCabin, class ticket.

the error i'm getting is :

 F:\credit.cpp In member function `void cruise::reserveCabin()':
270 F:\credit.cpp aggregate `ticket tic' has incomplete type and cannot be defined  

class ticket;
class cruise 
              char cabin[52][3][13][4];
              int num, Rb, Rw, Ri;
              char res,templ,tempc,tempr;
             int cru, lvl, row, col;
             friend class ticket;
             void readInformation(); 
             void writeInformation();
             void showAvailability();
             void writeAvailability(); 
             void reserveCabin();};    

void cruise::reserveCabin()
     string name;
     string ctype;
     int numpass;
     char res;
      cout << "Would you like to make a reservation? Y/N" << endl;
      cin >> res;
      while ((res == 'Y') || (res == 'y'))
          cout << "What is your name?" << endl;
          cin >> name;
          cout << endl;
             cout << "How many passengers? 1-4" << endl;
             cin >> numpass;
             cout << endl;
                cout << "Number of passengers must be between 1 and 4" << endl;
             cout << "Which cruise whould you like to reserve? 1-52" << endl;
             cin >> cru;
             cout << endl;
                cout << "Cruise must be between 1 and 52" << endl;
          while((lvl>3)||(lvl<1))//repeat until user has <strong class="highlight">input</strong> a level between 1-3
             cout << "Which Level Would you like to reserve? 1-3" << endl;
             cin >> lvl;
             cout << endl;
             if((lvl>3)||(lvl<1))//if user <strong class="highlight">input</strong> a number not between 1-3
                cout << "Level must be between 1 and 3, or you did not enter a number" << endl;
             cout << "Which Row Would you like to reserve? 1-13" << endl;
             cin >> row;
             cout << endl;
                cout << "Row must be between 1 and 13" << endl;
             cout << "Which Column Would you like to reserve? 1-4" << endl;
             cin >> col;
             cout << endl;
                cout << "Column must be between 1 and 4" << endl;

          lvl = lvl -1; //Minus 1 for array
          row = row -1; //Minus 1 for array
          col = col -1; //Minus 1 for array
          cru = cru -1; //Minus 1 for array

          if (cabin[cru][lvl][row][col] != 'X')
             cabin[cru][lvl][row][col] = 'X';
             //cout << cabin[cru][lvl][row][col] << endl;
             lvl = lvl + 1; //Plus 1 after array use
             row = row + 1; //Plus 1 after array use
             col = col + 1; //Plus 1 after array use
             cru = cru + 1; //Plus 1 after array use
             cout << "You have booked the Cabin on Level: " << lvl
                  << " Row: " << row
                  << " Colunm: " << col << endl;
             if((row<6) && (col==1)||(col==4))
                    cout <<"Row less then 6, col =1 or col 4" << endl;
                    ctype = "Balcony"; 
                  else if((row>5) && (col==1)||(col==4))
                    cout <<"Row greater then 6, col =1 or col 4" << endl;                       
                    ctype = "Window";
               else if((col==2)||(col==3))
                    cout <<"col = 2 or col = 3" << endl;
                    ctype = "Inside";

             ticket tic;
             tic.issueTicket(name,cru,lvl,row,col, cabtype, passno);                      }  
           cout << "That cabin is already booked" << endl;
           cout << "Would you like to try another cabin? Y/N" << endl;
           cin >> res;
           cout << endl;

class ticket
              int cruNo, level, row, col;
              string cabtype;
              string cusname;
              int passno;
              float total;
              friend class booking;
              friend class cruise;
               void issueTicket(string name, int cru, int lvl, int row, int col, string cabtype, int passno);
void issueTicket(string name, int cru, int lvl, int row, int col, string cabtype, int passno)
     float cost;
     cout << "-----Issue Ticket-----" << endl;
     cout << "Name: " << name << endl;
     cout << "Cruise Number: " << cru << endl;
     cout << "Level: " << lvl << endl 
          << "Row  " << row  << endl
          << "Column: " << col << endl;
     cout << "Cabin Type: " << cabtype << endl;
     cout << "Number of Passengers: " << passno << endl;
     cout << "Cost: " << cost << endl;
   ofstream ticket("ticket.txt");
     ticket << "-----Ticket-----" << endl;
     ticket << "Name: " << name << endl;
     ticket << "Cruise Number: " << cru << endl;
     ticket << "Level: " << lvl << endl 
            << "Row:  " << row  << endl
            << "Column: " << col << endl; 
     ticket << "Cabin Type: " << cabtype << endl;
     ticket << "Number of Passengers: " << passno << endl;    
     ticket << "Cost: " << cost << endl;                 

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi  FallenAngel1,

the problem is the forward declaration for class 'ticket' (line 1) has to be completed before the first instance can be created.

So, if you just move the declaration of the class 'ticket' to a place before the function where it's used (here 'cruise::reserveCabin') it should work - and you shouldn't need to declare the 'class ticket' as friend or even need a forward declaration, so the code should be fine somehow like this:

> class cruise { ... };
> class ticket { ... };
> void cruise::reserveCabin()
> ...

Hope that helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
The issue has nothing to do with friends (your functions are public), instead it looks like you've not provided the compiler with the definition of your class before trying to call functions on it.

At the top of the file you forward declare ticket, you need to include the full class definition here.
FallenAngel1Author Commented:
that worked but now i get the following errors:

 F:\credit.cpp In member function `void cruise::reserveCabin()':
285 F:\credit.cpp `cabtype' undeclared (first use this function)
  (Each undeclared identifier is reported only once for each function it appears in.)
 285 F:\credit.cpp `passno' undeclared (first use this function)

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

evilrixSenior Software Engineer (Avast)Commented:
tic.issueTicket(name,cru,lvl,row,col, cabtype, passno);

should probably be

tic.issueTicket(name,cru,lvl,row,col, ctype, numpass);
FallenAngel1Author Commented:
thanx for pointing that out...i didn't realize it...

um.. now errors i'm getting are:

  [Linker error] undefined reference to `ticket::issueTicket(std::string, int, int, int, int, std::string, int)'
  [Linker error] undefined reference to `booking::calTicket(int, std::string)'
  ld returned 1 exit status

wat does this mean?

evilrixSenior Software Engineer (Avast)Commented:
void issueTicket(string name, int cru, int lvl, int row, int col, string cabtype, int passno)

should be

void ticket::issueTicket(string name, int cru, int lvl, int row, int col, string cabtype, int passno)

You've probably done something similar with booking::calTicket
FallenAngel1Author Commented:
thank u.. it works now .....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.