Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “如何制作函数”;“运行”;工作程序在调用run时退出_C++_Oop - Fatal编程技术网

C++ “如何制作函数”;“运行”;工作程序在调用run时退出

C++ “如何制作函数”;“运行”;工作程序在调用run时退出,c++,oop,C++,Oop,我试图为质询(self)创建一个登录系统(正在进行中),但返回函数的函数“run”在调用时退出。好心协助 #include <iostream> #include <string> #include <array> #include <vector> using namespace std; //initialise function pointer that is void and takes no parameter typedef void

我试图为质询(self)创建一个登录系统(正在进行中),但返回函数的函数“run”在调用时退出。好心协助

#include <iostream>
#include <string>
#include <array>
#include <vector>

using namespace std;
//initialise function pointer that is void and takes no parameter
typedef void (*funcpointer)();
//forward declare a function run that takes an integer parameter
funcpointer run(int op);
//declare class user with username and password
class User {
private:
    string m_name;
    string m_password;

public:
    //constructor for class
    User()
    {
    }
    //friend functions that need to access class members
    friend istream& operator>>(istream& in, User& user);
    friend ostream& operator<<(ostream& out, User& user);
    friend void access();
    friend void display();
};
//vector that stores class
std::vector<User> m_user;
//allows user defined input of class members
istream& operator>>(istream& in, User& user)
{
    cout << "Enter your username: ";
    in >> user.m_name;
    cout << "Enter your password: ";
    in >> user.m_password;
    return in;
}
//ouputs Class member contents in user defined manner(allows cout << class)
ostream& operator<<(ostream& out, User& user)
{
    out << "Username is: " << user.m_name << " Password is: " << user.m_password << '\n';
    return out;
}
//allows user to choose whether to log in, make ne user or view users
void logIn()
{
    int num;
    cout << "Would you like to: \n1: Make new user\n2: Log In\n3: Display users\n";
    do {
        cin >> num;
    } while ((num != 1) && (num != 2) && (num != 3));

    run(num);
}
void newUser()
{
    User x;
    cin >> x;
    m_user.push_back(x);
}
void access()
{

    string name, password;
    cout << "Enter username: ";
    getline(cin, name);

    for (size_t i = 0; i < m_user.size(); i++) {
        if (name == m_user[i].m_name) {
            cout << m_user[i].m_name << " found. Enter password: ";
            getline(cin, password);
            if (password == m_user[i].m_password) {
                cout << "access granted";
            }
            else
                cout << "Wrong password\n";
        }
        else
            cout << "Username not found!\n";
    }
}
void display()
{
    int count = 1;
    for (auto& users : m_user) {
        cout << count << ": " << users.m_name << '\n';
    }
}
//function run that returns function
funcpointer run(int op)
{
    switch (op) {
    default:
    case 1:
        return newUser;
    case 2:
        return access;
    case 3:
        return display;
    }
}

int main()
{

    logIn();

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//初始化无效且不带参数的函数指针
typedef void(*funcpointer)();
//正向声明接受整数参数的函数运行
函数指针运行(int-op);
//使用用户名和密码声明类用户
类用户{
私人:
字符串m_name;
字符串m_密码;
公众:
//类的构造函数
用户()
{
}
//需要访问类成员的友元函数
friend istream和operator>>(istream和in、用户和用户);
friend ostream和操作员(istream和in、用户和用户)
{
cout>user.m_名称;
cout>user.m_密码;
返回;
}

//以用户定义的方式输出类成员内容(允许cout如果您返回您可能想要调用的funcpointer。您可以通过在funcpointer对象之后写入()来实现它,例如
run(num)(;

您的
LogIn()
不会对从run返回的指针执行任何操作。
run(num);
这样程序就结束了。请注意run()不运行任何操作。它只返回一个指向调用函数执行返回函数所需的函数的指针。在您的情况下,调用函数只是忽略/丢弃该指针。您的函数
run
只返回一个函数指针,但您从未调用指向的函数。请尝试
run(num)()
。请注意,无法解释为数字的无效输入(例如,如果包含字母)会导致
std::cin
进入错误状态。请参阅例如,如何退出。顺便问一下,为什么不直接在
run
函数中调用这些函数?
无效运行(int){案例1:newUser();break;}