eclipse - Accessing an object from another class C++ -


i trying create library system. have source file called 3.cpp, several classes called game.h, dvd.h, book.h, library.h , media.h.

i creating object called lib in 3.cpp , trying access lib object game class. how can this? using eclipse onmac os.

the 3.cpp source file is:

#include <stdlib.h> #include <iostream> #include <fstream> #include <string> #include "library.h" // include header of library class #include "game.h" // include header of game class #include "dvd.h" // include header of dvd class #include "book.h" // include header of book class using namespace std;  int main(){      library lib;      while( 1 ){          char mainselect;         char gameoption, name[30], platform[30], copies[10];         char dvdoption, director[30];         char bookoption, author[30];         char mainmenu;          // read user selection         cin.getline( name, 80);         mainselect = name[0];          // switch statement select between options         switch (mainselect){            case '1':               break;            case '2':                break;            case '3':                break;            case '4':                exit(0);                break;            case '5':                cout << "invalid selection!" << endl;                system("pause");                break;         }          if (mainselect == '1'){             cin.getline( name, 80);            dvdoption = name[0];          switch (dvdoption){             case '1':               cout << "enter name of dvd: ";               cin.getline( name, 80);               cout << "enter director of dvd: ";               cin.getline(director, 80);               cout << "enter no of copies: ";               cin.getline(copies, 80);               lib.insertdvd( name, director, atoi(copies));               break;            case '2':               cout << "enter name of dvd:\n";               cin.getline(name, 80);               lib.deletedvd(name);               break;            case '3':               cout << "enter name of dvd:\n";               cin.getline(name, 80);               dvd *item;               item = lib.searchdvd( name );               if( item != null){                 cout << "dvd found\n" << item->name << endl << item->director << endl << item->copies << endl;               }               else               cout << "dvd not found\n";               break;            case '4':               break;            case '5':               exit(0);               break;            case '6':               cout << "invalid selection!" << endl;               system("pause");               break;         }         }          else if (mainselect == '2'){           "i need add method here call gamemenu method game class."       return 0; } 

the game class code is:

#ifndef game_h_ #define game_h_  #include "media.h" #include "library.h" using namespace std;  class game : public media{ public:      char platform[45];     char gameoption, name[30], platform[30], copies[10];      void gamemenu(){ cout << "****************************************************" << endl;            cout << "*******************  game menu  ********************" << endl;            cout << "****************************************************" << endl;            cout << "*                                                  *" << endl;            cout << "* program          description                     *" << endl;            cout << "* ------------------------------------------------ *" << endl;            cout << "*                                                  *" << endl;            cout << "*   1              add new game                  *" << endl;            cout << "*                                                  *" << endl;            cout << "*   2              delete game                   *" << endl;            cout << "*                                                  *" << endl;            cout << "*   3              search game               *" << endl;            cout << "*                                                  *" << endl;            cout << "*   4              return previous menu     *" << endl;            cout << "*                                                  *" << endl;            cout << "*   5              exit                            *" << endl;            cout << "*                                                  *" << endl;            cout << "* ------------------------------------------------ *" << endl;            cout << "*                                                  *" << endl;            cout << "****************************************************" << endl;              cin.getline( name, 80);             gameoption = name[0];          switch (gameoption){             case '1':              cout << "enter name of game: ";              cin.getline( name, 80);              cout << "enter game platform: ";              cin.getline(platform, 80);              cout << "enter no of copies: ";              cin.getline(copies, 80);              lib.insertgame( name, platform, atoi(copies));              break;            case '2':              cout << "enter name of game:\n";              cin.getline(name, 80);              lib.deletegame(name);              break;            case '3':              cout << "enter name of game:\n";              cin.getline(name, 80);              game *item;              item = lib.searchgame( name );              if( item != null){              cout << "game found\n" << item->name << endl << item->platform << endl << item->copies << endl;              }              else              cout << "game not found\n";              break;              case '4':              exit(0);              break;            case '5':              cout << "invalid selection!" << endl;              system("pause");              break;         }     }  };   #endif // end of "#ifndef" block 

i getting errors try access lib object created in game class. errors are:

1. use of undefined identifier lib. 2. method insertgame not resolved. 3. method deletegame not resolved. 

the problems:

you have defined library lib; local variable of main(). means can access variable in main() else.

unfortunately, refer lib in member function gamemenu() belonging game class. `gamemenu() not see local variables of calling functions.

i try make simple: gamemenu()function lives in black box of class. can use own local variables, such item, class members, such gameoption. not variables of world outise black box.

the other errors consequences of first. example, in statement lib.insertgame(name, platform, atoi(copies));, compiler doesn't know lib is, complains, because doesn't know can object doesn't know.

there problem collection[numgames].copies have defined c-string in class, try manage int.

you have structural issue. it's not error, it's bad habit.
if put class in header, should put declaractions (data members , function declarations) , not function definitions.

solution

first simplify game.h header follows:

#ifndef game_h_ #define game_h_  #include "media.h" using namespace std;  class library;  // forward declaration, dbe detailed later  class game : public media{  // no code  public:     char platform[45];     char gameoption, name[30];        int copies;    // change numeric       void gamemenu(library& lib);  // pass argument called lib reference }; #endif // end of "#ifndef" block  

then put code in new game.cpp file:

#include "game.h" #include <iostream> using namespace std;  #include "library.h"  void game::gamemenu(library& lib)  // knows lib.  {     ...  // put code here     cin>>copies;   //  instead of getline()      lib.insertgame(name, platform, copies);  // no atoi() anymore, int      ... } 

now think overall structure of code: moment, there menu in 3.cpp , menu in game. it's logic in library, because these menu functions managing library object. makes less sense in game, choose.

finally, if you're learning c++, advise rid of c[] strings, strcpy(), strcmp() , alike , , opt c++ string


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -