C++ 函数指针生成“非静态成员函数的无效使用”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15756331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:45:04  来源:igfitidea点击:

function pointers generate 'invalid use of non-static member function' error

c++function-pointers

提问by rahman

I am trying to grasp pointer function concept in a better way. So I have a very simple and working example as:

我试图以更好的方式掌握指针函数的概念。所以我有一个非常简单和有效的例子:

#include <iostream>

using namespace std;

int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}

int main()
{
    int  a, b;
    int  (*plus)(int, int);
    int  (*minus)(int, int);
    plus = &add;
    minus = &subtract;
    a = operation(7, 5, add);
    b = operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

So far so good, Now I need to group the functions in a class, and select add or subtract based on the function pointer that i use. So I just make a small modification as:

到目前为止一切顺利,现在我需要将函数分组到一个类中,并根据我使用的函数指针选择加法或减法。所以我只是做了一个小的修改:

#include <iostream>

using namespace std;

class A
{
public:
int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};

int main()
{
    int  a, b;
    A a_plus, a_minus;
    int (*plus)(int, int) = A::add;
    int (*minus)(int, int) = A::subtract;
    a = a_plus.operation(7, 5, plus);
    b = a_minus.operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

and the obvious error is:

明显的错误是:

ptrFunc.cpp: In function ‘int main()':
ptrFunc.cpp:87:29: error: invalid use of non-static member function ‘int A::add(int, int)'
ptrFunc.cpp:88:30: error: invalid use of non-static member function ‘int A::subtract(int, int)'

coz I haven't specified which object to invoke(and I don't want to use static methods for now)

因为我还没有指定要调用哪个对象(我现在不想使用静态方法)

EDIT:several comments and answers suggested that the non-static version(as I have written) is not possible.(thanks to all) So, Modifying the class in the following manner also wont work:

编辑:一些评论和答案表明非静态版本(如我所写)是不可能的。(感谢所有人)因此,以下列方式修改类也行不通:

#include <iostream>

using namespace std;

class A
{
    int res;
public:
    A(int choice)
    {
        int (*plus)(int, int) = A::add;
        int (*minus)(int, int) = A::subtract;
        if(choice == 1)
            res = operation(7, 5, plus);
        if(choice == 2)
            res = operation(20, 2, minus);
        cout << "result of operation = " << res;
    }
int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};

int main()
{
    int  a, b;
    A a_plus(1);
    A a_minus(2);
    return 0;
}

generated this error:

产生了这个错误:

ptrFunc.cpp: In constructor ‘A::A(int)':
ptrFunc.cpp:11:30: error: cannot convert ‘A::add' from type ‘int (A::)(int, int)' to type ‘int (*)(int, int)'
ptrFunc.cpp:12:31: error: cannot convert ‘A::subtract' from type ‘int (A::)(int, int)' to type ‘int (*)(int, int)'

may I know how to solve this issue please?

我可以知道如何解决这个问题吗?

thanks

谢谢

采纳答案by Arun

The syntax to declare a function pointer to member methods is:

声明指向成员方法的函数指针的语法是:

int (A::*plus)(int, int) = &A::add;
int (A::*minus)(int, int) = &A::subtract;

To invoke member methods use .* or ->* operator:

要调用成员方法,请使用 .* 或 ->* 运算符:

 (a_plus.*plus)(7, 5);

Also have a look at http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx

也看看http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx

Hope this helps.

希望这可以帮助。

Complete code:

完整代码:

     #include <iostream>

    using namespace std;

    class A
    {
    public:
    int add(int first, int second)
    {
        return first + second;
    }

    int subtract(int first, int second)
    {
        return first - second;
    }

    int operation(int first, int second, int (A::*functocall)(int, int))
    {
        return (this->*functocall)(first, second);
    }
    };

    int main()
    {
        int  a, b;
        A a_plus, a_minus;
        int (A::*plus)(int, int) = &A::add;
        int (A::*minus)(int, int) = &A::subtract;
        a = a_plus.operation(7, 5, plus);
        b = a_minus.operation(20, a, minus);
        cout << "a = " << a << " and b = " << b << endl;
        return 0;
    }

回答by maditya

The edit you made to your code is still wrong because it doesn't make the member functions static. You need to make the add, subtract etc. functions static by adding the staticspecifier:

您对代码所做的编辑仍然是错误的,因为它不会使成员函数成为静态。您需要通过添加说明static符使加、减等函数成为静态:

#include <iostream>

using namespace std;

class A
{
    int res;
public:
    A(int choice)
    {
        int (*plus)(int, int) = A::add;
        int (*minus)(int, int) = A::subtract;
        if(choice == 1)
            res = operation(7, 5, plus);
        if(choice == 2)
            res = operation(20, 2, minus);
        cout << "result of operation = " << res;
    }
static int add(int first, int second)
{
    return first + second;
}

static int subtract(int first, int second)
{
    return first - second;
}

static int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};

回答by Pruthviraj

See the below code. The function calls are working without making them static.

请参阅下面的代码。函数调用在不使它们静态的情况下工作。

class A
{
  public:
  int add(int first, int second)
  {
      return first + second;
  }

  int subtract(int first, int second)
  {
      return first - second;
  }

  int operation(int first, int second, int(A::*functocall)(int, int))
  {
      return (this->*functocall)(first, second);
  }
};
//typedef int(A::*PFN)(int, int) ;
int main()
{
    int  a, b;
    A a_plus, a_minus;
    a = a_plus.operation(7, 5, &A::add);
    b = a_minus.operation(20, a, &A::subtract);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

回答by Wintermute

You can't pass non-static member function as argument that easy. And for your needs, I believe it's better to override operators: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

您不能那么容易地将非静态成员函数作为参数传递。并且根据您的需要,我认为最好覆盖运算符:http: //www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

But if you really need them as actual member functions - just make them static.

但是如果你真的需要它们作为实际的成员函数 - 只需将它们设为静态即可。