struct具有成员函数作为参数 [英] struct with member function as parameter

查看:239
本文介绍了struct具有成员函数作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者在C + +和堆栈交换。我正在一个接口类,获得键盘输入和检查,以查看是否是正确的通过循环结构的数组包含要比较的字符串和字符串输出,如果它等于比较字符串。如果输入正确,它将打印结构中的字符串,并调用结构中的函数并执行某些操作。



interface.hpp

  #include< string> 
class Input_Interface {
struct command_output {
std :: string command;
std :: string success_string;
std :: string failure_string;
void output_function();
}

bool stop_loop = false;
void Clear();
void Quit_loop();

};

interface.cpp

  #include< iostream> 
void Input_Interface :: Quit_loop(){
stop_loop = true;
//结束循环并关闭程序
}

void Input_Interface :: clear(){
//执行某个操作
}

Input_Interface :: command_output clear_output {CLEAR,CLEARED,,Input_Interface :: Clear()};
Input_Interface :: command_output quit_output {QUIT,GOODBYE,,Input_Interface :: Quit_loop()};
Input_Interface :: command_output output_arr [] {clear_output,quit_output};

void Input_Interface :: begin(){
while(stop_loop == false){
Input_Interface :: get_input(); //将输入存储到var中,调用input_str(稍后显示)
this-> compare_input();
}
}

void Input_Interface :: compare_input(){
for(unsigned int i = 0; i <2; i ++){
if (this-> input_str == output_arr [i] .command){
std :: cout< output_arr [i] .success_string<< std :: endl;
output_arr [i] .output_function();
break;
}
}
// ...如果没有匹配command_output结构数组中的任何命令字符串,则通过else for失败打印无效
  

Input_Interface :: command_output clear_output {CLEAR,CLEARED,,Input_Interface :: Clear()};
//错误:调用非静态函数不带对象参数

Input_Interface :: command_output quit_output {QUIT,GOODBYE,,Input_Interface :: Quit_loop ;
//错误:调用不带对象参数的非静态函数

em> this 是通过类的成员函数传递的,但我不知道如何解决这个问题。我不能真正地确定问题是结构对象内的范围解析操作符还是不是因为我可以使用它外面的参数只是罚款。



任何帮助将是非常感谢。

解决方案

您应该执行以下操作:

  #include< string> 
struct Input_Interface {
struct command_output {
std :: string command;
void(* output_function)();
};

static void Clear();
static void Quit_loop();
};

int main(){
Input_Interface :: command_output t = {CLEAR,Input_Interface :: Clear};
return 0;
}



虽然我建议使用函数对象函数指针。


I am a beginner in C++ and stack exchange. I am working on an Interface class that gets keyboard input and checks to see whether it is correct through looping through an array of structs which contains strings to compare to and strings to output depending if it is equal to the compare string or not. If the input is correct, it will print the string within the struct and a function within the structure is called and does some action.

interface.hpp

#include <string>
class Input_Interface {
    struct command_output {
    std::string command;
    std::string success_string;
    std::string failure_string;
    void output_function();
    }

    bool stop_loop = false;
    void Clear();
    void Quit_loop();

};

interface.cpp

#include <iostream>
void Input_Interface::Quit_loop() {
   stop_loop = true;
   // ends loop and closes program
}

void Input_Interface::clear() {
   // does some action
}

Input_Interface::command_output clear_output{"CLEAR", "CLEARED", "", Input_Interface::Clear()};
Input_Interface::command_output quit_output{"QUIT", "GOODBYE", "", Input_Interface::Quit_loop()};
Input_Interface::command_output output_arr[]{clear_output, quit_output};

void Input_Interface::begin() {
while (stop_loop == false) {
    Input_Interface::get_input(); //stores input into var called input_str shown later
    this->compare_input();
    }
}

void Input_Interface::compare_input() {
for (unsigned int i=0; i<2; i++) {
    if (this->input_str == output_arr[i].command) {
        std::cout << output_arr[i].success_string << std::endl;
        output_arr[i].output_function();
        break;
        }
    }
    // ... goes through else for failure printing invalid if it did not match any of the command string in the command_output struct array

My issue is with these lines

Input_Interface::command_output clear_output{"CLEAR", "CLEARED", "", Input_Interface::Clear()};
//error: call to non-static function without an object argument

Input_Interface::command_output quit_output{"QUIT", "GOODBYE", "", Input_Interface::Quit_loop()};
//error: call to non-static function without an object argument

I know this is passed through member functions of the class but I don't know how to go about fixing this problem. I'm not really sure if the problem is the scope resolution operator inside the struct object or not because I can use it outside of parameters just fine.

Any help would be greatly appreciated.

解决方案

You should do something as shown below:

#include <string>
struct Input_Interface {
    struct command_output {
      std::string command;
      void (*output_function)();
    };

    static void Clear();
    static void Quit_loop();
};

int main() {
    Input_Interface::command_output t = {"CLEAR", Input_Interface::Clear};
    return 0;
}

Live example here

Although I would suggest using a functor object over function pointer.

这篇关于struct具有成员函数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆