運行此代碼時出現此錯誤。請查看我的代碼並提供幫助。無法將參數1從'int(__thiscall A :: * *)(void)'轉換爲'int(__cdecl *)(void)'
#include "stdafx.h"
#include <iostream>
class A
{
public:
void PrintTwoNumbers(int (*numberSource)(void))
{
int val1= numberSource();
}
int overNineThousand(void)
{
return (rand()%1000) + 9001;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int (A::*fptr) (void) = &A::overNineThousand;
int (A::*fptr1) (void);
fptr1 = &A::overNineThousand;
A a;
a.PrintTwoNumbers(&fptr); //-> how to pass here
getchar();
return 0;
}
我通過搜索這個網上,並且沒有人給了this.Can任何一個完美的解決方案編輯該代碼爲工作代碼和幫助我厭倦了?
非靜態成員函數與靜態成員函數,全局函數或名稱空間函數不同。非靜態成員函數的不同之處在於它們需要調用一個對象,該對象作爲'this'指針不可見地傳遞給函數。我建議你閱讀['std :: function'](http://en.cppreference.com/w/cpp/utility/functional/function)和['std :: bind'](http:// en。 cppreference.com/w/cpp/utility/functional/bind)。最後,請勿使用不相關的語言標籤進行垃圾郵件。 –
[使用C++類成員函數作爲C回調函數](https://stackoverflow.com/questions/1000663/using-ac-class-member-function-as-ac-callback-function) –
@ Someprogrammerdude:我使用的是visual studio 2008,所以std :: function和std :: bind不可用 – shivcena