• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • Last Modified:

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 
{
      private:
              char cabin[52][3][13][4];
              int num, Rb, Rw, Ri;
              char res,templ,tempc,tempr;
      public:
             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;
     numpass=0;
      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;
          while((numpass>4)||(numpass<1))
          {
             cout << "How many passengers? 1-4" << endl;
             cin >> numpass;
             cout << endl;
             if((numpass>4)||(numpass<1))
                cout << "Number of passengers must be between 1 and 4" << endl;
          }
          
          while((cru>52)||(cru<1))
          {
             cout << "Which cruise whould you like to reserve? 1-52" << endl;
             cin >> cru;
             cout << endl;
             if((cru>52)||(cru<1))
                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;
          }
          while((row>13)||(row<1))
          {
             cout << "Which Row Would you like to reserve? 1-13" << endl;
             cin >> row;
             cout << endl;
             if((row>12)||(row<1))
                cout << "Row must be between 1 and 13" << endl;
          }
          while((col>4)||(col<1))
          {
             cout << "Which Column Would you like to reserve? 1-4" << endl;
             cin >> col;
             cout << endl;
             if((col>4)||(col<1))
                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";
               }

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


class ticket
{
      private:
              int cruNo, level, row, col;
              string cabtype;
              string cusname;
              int passno;
              float total;
              friend class booking;
              friend class cruise;
      public:  
               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;                 
   ticket.close();
}

Open in new window

0
FallenAngel1
Asked:
FallenAngel1
  • 3
  • 3
3 Solutions
 
ZoppoCommented:
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,

ZOPPO
0
 
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.
0
 
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)

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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);
0
 
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?

0
 
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
0
 
FallenAngel1Author Commented:
thank u.. it works now .....
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now