【问题标题】:Call to non-static member function without an object argument error [duplicate]在没有对象参数错误的情况下调用非静态成员函数[重复]
【发布时间】:2015-01-26 06:50:41
【问题描述】:

谁能解释我为什么会收到这个错误?

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

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;
};

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

我的问题在于这些行

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

我知道 this 是通过类的成员函数传递的,但我不知道如何解决这个问题。我不确定问题是否是导致错误的结构对象内的范围解析运算符,因为我可以在结构之外使用它就好了。 Clear() 和 Quit() 不能是静态的,因为它必须能够访问类中的对象。

【问题讨论】:

  • 函数不是 C++ 中的对象;您不能分配功能。也许您正在寻找函数指针或指向成员函数的指针?
  • 函数确实是 C++ 中的对象,您可能会想到 C。您可以使用 std::function 分配模板函数。我已经尝试将其用作 std::function 而不是将 Clear() 和 Quit_loop() 声明为 void 函数,但我也无法让它工作。如果我需要这些函数在我的类中工作,我不确定指针如何解决这个问题。
  • std::is_object 不同意你的观点。
  • 我想你是对的。我所说的是Functors(函数对象),它不同于函数,你可以让函数表现为对象。
  • 我很清楚这一点(确实会出现在答案中)。我只是指出为什么您的代码不起作用。

标签: c++ class struct scope member-functions


【解决方案1】:

如果我没记错的话,struct command_output 中的output_function 应该存储class Input_Interface 的成员函数。

你需要改变这个:

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)();
}

然后,更改初始化实例的方式:

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 clear_output{"CLEAR", "CLEARED", "", &Input_Interface::Clear};
Input_Interface::command_output quit_output{"QUIT", "GOODBYE", "", &Input_Interface::Quit_loop};

最后正确调用它们,改变这个:

output_arr[i].output_function();

进入这个:

(this->*output_arr[i].output_function)();

错误来自您正在编写Input_Interface::Clear()Input_Interface::Quit_loop()

这个符号意味着两件事:括号表示要评估函数,并将结果传递给结构初始化器,这是不可能的,因为它们返回 void 并且这些函数被调用就好像它们是静态成员函数一样,事实并非如此。

【讨论】:

  • 最后一段漏掉了很多要点:语法,即使它是有效的,也会评估函数调用,而不是表达函数本身。跨度>
  • 我试图用我的编辑来表达,这样更好吗?我很难解释这两个问题......如果你能让它更清晰、更准确,请随时编辑它。
  • 是的,它更好,虽然我认为真正重要的一点是“函数”和“调用函数的结果”之间的区别。但我想这已经足够清楚了。干杯。
  • 现在我明白了 Kerrek SB 使用函数指针的含义。谢谢你们的帮助。感谢 Chnossos 的完整解释!
  • 是的,在这里转换它确实感觉很奇怪,但我只是按照我认为我的教授所说的话。我将不得不仔细检查。我正在尝试编写一个单独的函数来转换它,但我不确定何时调用它。我几乎认为在重载函数中调用它是有意义的,因此它是在编写之前发生的最后一件事,但我无法让函数调用在重载函数中工作。
猜你喜欢
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 2012-02-17
  • 1970-01-01
  • 2011-10-17
  • 2011-05-31
  • 1970-01-01
相关资源
最近更新 更多