如何在c ++中调用全局函数中的类对象?

时间:2017-06-13 12:44:59

标签: c++ object

我有一个全局函数void start_menu(),我将其用作界面。

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;,上面的代码给了我这个错误:

  

错误:&#39;图书&#39;未在此范围内宣布。

这是全局函数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(); 
};

1 个答案:

答案 0 :(得分:0)

基于评论,我在Books全局函数之后应该调用void start_menu()类,这是不可能的。