成员函数在主函数中可访问,但在 while 循环中抛出错误"not declared in this scope"

Member function accessible in main function, but in while loop throws error "not declared in this scope"

我正在尝试制作一个简单的循环菜单。我将成员函数 mainMenu() 移到了 main() class 上面,正如互联网上建议的那样

但是当我尝试从 while 循环中调用 mainMenu() 时,编译器给出了以下错误,如注释中所述:

'mainMenu' was not declared in this scope

下面是头文件。我一直在搞乱类型,试图让错误消失,我不知道这是否能解决任何问题。如果mainMenu()不是成员函数,那么什么是成员函数?

Main.h

class main
{
    private:
    //variables
    bool quit;
    int userSelect;

    //constructor
    main();

    //methods
    mainMenu();
};

main.cpp

#include <iostream>

#include "Main.h"
using namespace std;

int main() {

bool quit = false;
int userSelect;

//mainMenu(); //** placing it here does not cause any errors **
while(!quit) {
    //Main Screen
    mainMenu(); // **actually I want it here, but I get the above error **
    cin >> userSelect;

    cin.ignore();
    switch (userSelect) {
        case '1':
            //moveInventory
            break;
        case '2':
            //viewInventory
            break;
        case '3': //user wants to exit
            //exit
            break;
        default:
            cout << "Command not recognized";
            mainMenu();
            break;
        }
    }

    return 0;
} //end of main

void mainMenu() {
    cout << "Welcome to the Simple Distribution Center.\n"
            "1) Move Inventory\n"
            "2) View Inventory\n"
            "3) Save and Exit" << endl;
}

不要使用 main 作为 class 名称,创建您想要的成员函数 public 并使用完整签名

userIO.h:

class userIO        // using `userIO` so its not confusing
{
private:
  // variables -- NONE

public:             // you need member functions public so they can be called
  enum choice { moveInventory=1, viewInventory=2, QUIT=3 }; // QUIT must be last

// constructor Not Needed

// static methods, since class has no data
  static void mainMenu();   // you need a full signature, *void*
  static choice userChoice();
};

并且您必须使用 :: 告诉编译器您的代码属于您的 class 请注意,所有 iostream 都包含在这里,最好将 using namespace std; 也隔离在这里

userIO.cpp:

#include <iostream>
#include "userIO.h"

using namespace std;

void userIO::mainMenu() {
    cout << "Welcome to the Simple Distribution Center.\n"
            "1) Move Inventory\n"
            "2) View Inventory\n"
            "3) Save and Exit" << endl;
}

userIO::choice userIO::userChoice() {
  int userSelect;

  while(1) {
    cin >> userSelect;
    cin.ignore();

    if ((userSelect < moveInventory) || (userSelect > QUIT)) {
      cout << "Command not recognized" << endl;
      mainMenu();  // Note no need for userIO:: since it's the same class
    } else {
      return choice(userSelect) ;
    }
  }
}

main.cpp:

#include "userIO.h"

int main() {
  bool quit = false;

  while(!quit) {
    userIO::mainMenu(); 
    switch (userIO::userChoice()) {
        case userIO::moveInventory :
            // code to move inventory here
            break;
        case userIO::viewInventory :
            // code to view inventory here
            break;
        case userIO::QUIT :
            quit=true;
            break;
        // NOTE: no default needed since only valid choices are returned
        }
    }

    return 0;
}