在main函数中可以访问的成员函数,但在while循环中抛出错误"未在此范围内声明"

时间:2015-08-25 18:11:03

标签: c++ menu while-loop member-functions

我试图制作一个简单的循环菜单。我将成员函数mainMenu()移到main()类之上,如互联网建议的那样

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

  

' MAINMENU'未在此范围内声明

以下是头文件。我一直在搞乱试图让错误消失的类型,我不知道这是否会清除任何东西。如果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;
}

1 个答案:

答案 0 :(得分:1)

不要将main用作类名,使您想要的成员函数称为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();
};

你必须使用::告诉编译器你的代码属于你的类 请注意,此处包含所有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;
}