Sekkmer Sekkmer - 1 month ago 15
C++ Question

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

I was trying to call a function with a function pointer where the function pointer is an class member. I simplifed the code to only show the problem but the foo() function in the oiginal code is already writen so I can not change that.

(In my main code foo is from GLFW (glfwSetKeyCallback) and A is an input handler class.)

#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); // work 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

Answer

The type of the expression a->testInA is a pointer to a non-static member function.

It does not have the same type as fptr, so the compiler emits an error, albeit a cryptic one.

Compilation would pass if testInA were static.

See C++ function pointer (class member) to non-static member function for more details.