明显调用的表达式前的括号必须具有(指针)函数类型 编译器错误 C2064

时间:2024-12-09 15:56:13

A call is made to a function through an expression.The expression does not evaluate to a pointer to a function that takes the specified number of arguments.

In this example, the code attempts to call non-functions as functions.The following sample generates C2064:

// 
int i, j;
char* p;
void func() {
	j = i();    // C2064, i is not a function
	p();        // C2064, p doesn't point to a function
}

You must call pointers to non-static member functions from the context of an object instance.The following sample generates C2064, and shows how to fix it:

// 
struct C {
	void func1() {}
	void func2() {}
};

typedef void (C::*pFunc)();

int main() {
	C c;
	pFunc funcArray[2] = { &C::func1, &C::func2 };
	(funcArray[0])();    // C2064 
	(c.*funcArray[0])(); // OK - function called in instance context
}

Within a class, member function pointers must also indicate the calling object context.The following sample generates C2064 and shows how to fix it:

// 
// Compile by using: cl /c /W4 
struct C {
	typedef void (C::*pFunc)();
	pFunc funcArray[2];
	void func1() {}
	void func2() {}
	C() {
		funcArray[0] = &C::func1;
		funcArray[1] = &C::func2;
	}
	void func3() {
		(funcArray[0])();   // C2064
		(this->*funcArray[0])(); // OK - called in this instance context
	}
};