Я пытаюсь создать библиотечную систему. У меня есть исходный файл под названием 3.cpp, несколько классов под названием Game.h, DVD.h, Book.h, Library.h и Media.h.Доступ к объекту из другого класса C++
Я создаю объект под названием lib в 3.cpp, и я пытаюсь получить доступ к объекту lib из класса Game. Как я могу это сделать? Я использую Eclipse onMac os.
Источник 3.cpp файл:
#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;
}
Код класса Игра:
#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
Я также получаю некоторые ошибки, когда я пытаюсь получить доступ к библиотеке объект, созданный в игре класс. Ошибки я получаю:
Там слишком много кода здесь, пожалуйста, изолировать проблему минимальный пример (менее 10 или 20 строк кода). Здесь слишком много классов и файлов, и сложно понять, в чем проблема ... – jpo38
Привет, я редактировал сообщение. – user3472448