Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.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++ 在没有对象参数错误的情况下调用非静态成员函数_C++_Class_Struct_Scope_Member Functions - Fatal编程技术网

C++ 在没有对象参数错误的情况下调用非静态成员函数

C++ 在没有对象参数错误的情况下调用非静态成员函数,c++,class,struct,scope,member-functions,C++,Class,Struct,Scope,Member Functions,有人能解释我为什么会犯这个错误吗 我正在处理一个接口类,它获取键盘输入,并通过循环一个结构数组来检查它是否正确,该数组包含要比较的字符串和要输出的字符串,具体取决于它是否等于比较字符串。如果输入正确,它将在结构中打印字符串,并调用结构中的函数并执行某些操作 interface.hpp #include <string> class Input_Interface { public: struct command_output { std::string command

有人能解释我为什么会犯这个错误吗

我正在处理一个接口类,它获取键盘输入,并通过循环一个结构数组来检查它是否正确,该数组包含要比较的字符串和要输出的字符串,具体取决于它是否等于比较字符串。如果输入正确,它将在结构中打印字符串,并调用结构中的函数并执行某些操作

interface.hpp

#include <string>
class Input_Interface {
public:
    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();

private:
    std::string input_str;
};

我知道这是通过类的成员函数传递的,但我不知道如何解决这个问题。我不确定问题是否是导致错误的struct对象内部的scope resolution操作符,因为我可以在struct外部使用它。Clear()和Quit()不能是静态的,因为它必须能够访问类中的对象。

如果我没有弄错的话,
struct命令\u output
中的
output\u函数
应该存储
类输入\u接口
的成员函数

您需要更改以下内容:

struct command_output {
    std::string command;
    std::string success_string;
    std::string failure_string;
    void output_function();
}
为此:

struct command_output {
    std::string command;
    std::string success_string;
    std::string failure_string;
    void (Input_Interface::*output_function)();
}
(this->*output_arr[i].output_function)();
然后,更改初始化实例的方式:

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

最后正确地调用它们,更改如下:

output_arr[i].output_function();
为此:

struct command_output {
    std::string command;
    std::string success_string;
    std::string failure_string;
    void (Input_Interface::*output_function)();
}
(this->*output_arr[i].output_function)();

错误源于您正在编写
Input\u Interface::Clear()
Input\u Interface::Quit\u loop()


< P>符号表示两个事物:括号表示函数要被评估,结果传递给结构初始化器,因为它们返回空格,这是不可能的,这些函数被称为静态成员函数,而不是这样。C++函数不是C++中的对象;不能分配函数。也许您正在寻找函数指针,或者指向成员函数?函数的指针实际上是C++中的对象,您可能想到C.,可以用STD::Audio函数分配模板函数。我已经尝试将它用作std::function,而不是将Clear()和Quit_loop()声明为void函数,但我也无法让它工作。如果我需要这些函数在我的类中工作,我也不确定指针如何解决这个问题。我不同意你的意见。我想你是对的。我所说的是函子(函数对象),它不同于函数,你可以让函数像对象一样运行。我很清楚这一点(事实上,答案是这样的)。我只是指出了你的代码不起作用的原因。最后一段忽略了很多要点:语法,即使它是有效的,也会计算函数调用,而不是表达函数本身。我试图用我的编辑来表达这一点,这更好吗?我很难解释这两个问题。。。如果你能让它更清晰、更准确,请随意编辑。是的,它更好,尽管我认为真正重要的是“函数”和“调用函数的结果”之间的区别。但我想这已经足够清楚了。干杯。现在我明白了Kerrek SB使用函数指针的意思。谢谢你们两位的帮助。感谢您对Chnossos的完整解释!是的,在这里转换它确实感觉很奇怪,但我只是按照我认为我的教授所说的去做。我得再检查一遍。我试图编写一个单独的函数来转换它,但我不确定何时调用它。我几乎认为在重载函数中调用它是有意义的,因此这是在编写它之前发生的最后一件事,但我无法让函数调用在重载函数中工作。
(this->*output_arr[i].output_function)();