-2

I am trying to create a Library system. I have a source file called 3.cpp, several classes called Game.h, DVD.h, Book.h, Library.h and Media.h.

i am creating an object called lib in 3.cpp and I am trying to access the lib object from the Game class. How can I do this? I am 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 to select between the 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 to add a method here to call the GameMenu method from the 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 a new Game                  *" << endl;
           cout << "*                                                  *" << endl;
           cout << "*   2              Delete a Game                   *" << endl;
           cout << "*                                                  *" << endl;
           cout << "*   3              Search for a Game               *" << endl;
           cout << "*                                                  *" << endl;
           cout << "*   4              Return to the 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 am also getting some errors as I try to access the lib object created in the Game class. The errors I get are:

1. Use of undefined identifier lib.
2. Method insertGame could not be resolved.
3. Method deleteGame could not be resolved.
user3472448
  • 33
  • 2
  • 9
  • 1
    There's too much code here, please isolate your problem in a minimal example (less than 10 or 20 lines of code). There are too many classes and files here and it's hard to figure out what your problem is... – jpo38 Mar 20 '15 at 20:47

1 Answers1

3

The problems:

You have defined Library lib; as local variable of main(). This means that you can access to this variable in main() but nowhere else.

Unfortunately, you refer to lib in the member function GameMenu() belonging to the Game class. `GameMenu() does not see the local variables of the calling functions.

I try to make it simple: the GameMenu()function lives in the black box of its class. It can use its own local variables, such as item, as well as the class members, such as gameOption. But not the variables of the world outise the black box.

The other errors are only consequences of the first. For example, in your statement lib.insertGame(name, platform, atoi(copies));, as the compiler doesn't know what lib is, he complains, because he doesn't know what he can do with an object that it doesn't know.

There is also another problem with collection[numGames].copies that you have defined as a c-string in the class, but try to manage like an int.

You have also a structural issue. It's not an error, but it's a bad habit.
If you put a class in a header, you should put only its declaractions (data members and function declarations) and not the function definitions.

Solution

First simplify the Game.h header as follows:

#ifndef GAME_H_
#define GAME_H_

#include "Media.h"
using namespace std;

class Library;  // forward declaration, will dbe detailed later 
class Game : public Media{  // no code 
public:
    char platform[45];
    char gameOption, name[30];   
    int copies;    // change to numeric 

    void GameMenu(Library& lib);  // pass an argument called lib by reference
};
#endif // end of "#ifndef" block 

Then put the code in a new Game.cpp file:

#include "Game.h"
#include <iostream>
using namespace std;

#include "Library.h"

void Game::GameMenu(Library& lib)  // now he knows lib. 
{
    ...  // put your code here
    cin>>copies;   //  instead of getline() 
    lib.insertGame(name, platform, copies);  // no atoi() anymore, its already an int 
    ...
}

Now think about the overall structure of your code: for the moment, there is a menu in 3.cpp and a menu in Game. It's logic in Library, because these menu functions are really about managing a library object. It makes less sense in Game, but up to you to choose.

Finally, if you're learning c++, I strongly advise you to get rid of your c[] strings, strcpy(), strcmp() and alike , and opt for c++ string

Community
  • 1
  • 1
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Why am I creating another .cpp file. And do I do this for everything such as DVD, Books?? – user3472448 Mar 24 '15 at 21:05
  • How can I put the menus for game, dvd, and books in the library?? – user3472448 Mar 24 '15 at 21:50
  • Because it's a good pracice to put implementation of member functions in a cpp, and definition of the class only in the header. Another reason is that your class definition doesn't rely on full Library definition, whereas the member's definition does. Hence, if you would keep everything in a single header, you'd need to includ eLibrary, which needs to include your header, which needs.... You'd have a circular dependency that will prevent compilation. – Christophe Mar 24 '15 at 22:04