如何在C++中调用全局函数中的类对象?
问题描述:
我有一个全局函数void start_menu()
,我用它作为接口。如何在C++中调用全局函数中的类对象?
void start_menu()
{
int x;
cout << " ------------------------------------" << endl;
cout << " WELCOME TO LIBRARY MANAGEMENT SYSTEM" << endl;
cout << "------------------------------------" << endl;
cout << " 1. ABOUT Books " << endl;
cout << " 2. ABOUT Members " << endl;
cout << " CHOOSE:";
cin >> x;
Books MyBooks; //object of books class
do
{
if (x==1)
{
system("cls");
MyBooks.INTR_Books(); //calling function of Books Class
}
};
}
然后,我有class Books{}
,我想在全局函数void start_menu()
被调用,但是当我做Books
类,它被定义为Books MyBooks;
的对象,上面的代码给了我这个错误:
error: 'Books' was not declared in this scope.
这是后Books
类全局函数void start_menu()
:
class Books
{
public:
string BookName; //name of the Book
string Auth; //Author of the book
string Trans; // translator of the book
string myArray[20];
int BookCode; // code of the book
int BookNum; // number of copies exist
void INTR_Books(); //show interface related to books
void ADD_BOOK();
void DELETE_BOOK();
void SEARCH();
void SHOW_ALL();
void BR_BOOK();
};
答
基于在评论中,我曾经在void start_menu()
全局函数之后调用Books
类。
在函数之前或之后是否声明了'Books'? – NathanOliver
@NathanOliver,它也需要定义。 – Incomputable
@Incomputable是的,但可以来自后者或来自其他文件。 – NathanOliver