明顯調用的表達式前的括號必須具備(指針)函數類型 編譯器錯誤 C2064

看到「明顯調用的表達式前的括號必須具備(指針)函數類型」這句時我才發現個人語文水平有多爛,怎麼看都看不懂,折騰了半天才知道是哪裏出了問題。html

舉個簡單的例子express

class CTest
{
	void (CTest::*m_pFun)();
	
	void CallFun()
	{
		(this->*m_pFun)();	//OK,對象指針和函數名必定要用括號括起來,函數名前面要加上*號
		this->*m_pFun();	//error
		(this->m_pFun)();	//error
	}
        //本文連接http://www.cnblogs.com/vcpp123/p/5902839.html
};

 

詳細說明請參閱MSDN,連接:https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k(C2064)&rd=trueapp

編譯器錯誤 C2064

term does not evaluate to a function taking N arguments函數

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.ui

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

// C2064.cpp
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:lua

// C2064b.cpp
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:spa

// C2064d.cpp
// Compile by using: cl /c /W4 C2064d.cpp
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
	}
};
相關文章
相關標籤/搜索