c++ - Visual Studio 'non-standard syntax; use ' &' to create a pointer to member'

标签 c++ visual-studio function class pointers

<分区>

我试图用一个函数指针调用一个函数,其中函数指针是一个类成员。

我简化了代码以仅展示问题,但是原始代码中的 foo() 函数已经写好了,所以我无法更改它。

(在我的实际代码中,foo 来自 GLFW(glfwSetKeyCallback),A 是输入处理程序类。)

代码是:

    #include <iostream>

    // this four line can not be changed
    typedef void(*fptr)(int);
    void foo(fptr f) {
        f(0);
    }

    void testOutA(int i) {
    }

    class A {
    public:
        void testInA(int i) {
        }
    };

    int main()
    {
        foo(testOutA); // works fine as it should be

        A * a = new A();

        foo(a->testInA); // ERROR

        return 0;
    }

编译错误信息为:

ERROR: error C3867: 'A::testInA': non-standard syntax; use '&' to create a pointer to member

最佳答案

表达式a->testInA类型 是指向非static 成员函数的指针。

它与 fptr 的类型不同,因此编译器会发出一个错误,尽管是一个神秘的错误。

如果 testInAstatic,编译将通过。

参见 C++ function pointer (class member) to non-static member function了解更多详情。

关于c++ - Visual Studio 'non-standard syntax; use ' &' to create a pointer to member',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40156662/

相关文章:

javascript - onclick 功能不起作用(更改样式)

c++ - 有没有办法获取函数的大小并为其复制和执行分配内存?

c++ - C++ 方法名称可以在 header 中由类名限定吗?

visual-studio - VS2010 : Why do my custom Toolbox tabs and contained controls keep disappearing?

带有参数语法错误的MySQL函数

visual-studio - Visual Studio 2013 许可证产品 key

c++ - 如何使用 Visual Studio 2008 确定 C++ 代码的复杂性(如 CPU 周期)

c++ - vsnprintf 文档中的 "encoding error"是什么意思?

c++ - 调用 cvSet2D() 设置图像的像素值返回值 0

c++ - 将 boost::uuid 转换为 char*