访问另一个类C++中的对象
Accessing an object from another class C++
我正在尝试创建一个库系统。我有一个名为3.cpp的源文件,有几个类名为Game.h、DVD.h、Book.h、Library.h和Media.h。
我正在3.cpp中创建一个名为lib的对象,并试图从Game类访问lib对象。我该怎么做?我在Mac操作系统上使用Eclipse。
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 foundn" << item->name << endl << item->director << endl << item->copies << endl;
}
else
cout << "DVD not foundn";
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 foundn" << item->name << endl << item->platform << endl << item->copies << endl;
}
else
cout << "Game not foundn";
break;
case '4':
exit(0);
break;
case '5':
cout << "Invalid selection!" << endl;
system("pause");
break;
}
}
};
#endif // end of "#ifndef" block
在尝试访问Game类中创建的lib对象时,我也遇到了一些错误。我得到的错误是:
1. Use of undefined identifier lib.
2. Method insertGame could not be resolved.
3. Method deleteGame could not be resolved.
问题:
您已经将Library lib;
定义为main()
的局部变量。这意味着您可以在main()中访问此变量,但不能访问其他变量。
不幸的是,您在属于Game
类的成员函数GameMenu()
中引用了lib
`GameMenu()看不到调用函数的局部变量。
我试着让它变得简单:GameMenu()
函数存在于它类的黑盒中。它可以使用自己的局部变量,如item
,也可以使用类成员,如gameOption
。但并不是世界的变量超过了黑匣子。
其他错误只是第一个错误的后果。例如,在您的语句lib.insertGame(name, platform, atoi(copies));
中,由于编译器不知道lib
是什么,他抱怨道,因为他不知道如何处理它不知道的对象。
collection[numGames].copies
还有另一个问题,您已经将其定义为类中的c字符串,但尝试像int
一样进行管理。
你还有一个结构性问题。这不是一个错误,但这是一个坏习惯
如果你把一个类放在头中,你应该只放它的声明(数据成员和函数声明),而不是函数定义。
解决方案
首先简化Game.h
标头如下:
#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
然后将代码放入新的Game.cpp
文件:
#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
...
}
现在想想代码的整体结构:目前,3.cpp
中有一个菜单,Game
中有一个子菜单。这是Library
中的逻辑,因为这些菜单函数实际上是关于管理库对象的。这在Game
中没有什么意义,但取决于您的选择。
最后,如果您正在学习c++,我强烈建议您去掉c[]
字符串、strcpy()
、strcmp()
等,选择c++string
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象