平安2010
[新手]
c++編譯錯
50分class Man
{
private:
typedef struct sAct{
void (*Func)();
sAct(){Func = NULL;}
}sAct;
protected:
sAct *m_Act;
public:
Man(){m_Act = NULL;}
void Move(){;}
void ActionSet(void (*Func)()){;}
};
int main()
{
Man man;
man.ActionSet(man.Move);
return 0;
}
這個程序編譯後,錯誤是:
C:/Documents and Settings/microsoft/桌面/test/main.cpp(21)
: error C2664: 'ActionSet' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'
None of the functions with this name in scope match the target type
把void Move(){;}改成static void Move(){;}後編譯成功,爲什麼啊?
如果你想保存成員函數爲一般函數指針,並像普通函數那樣用,那就只有聲明其爲靜態一種方式。
另外針對你的例子,不改成static的方式是如下的:
#include <stdio.h>
class Man
{
private:
typedef struct sAct{
void (*Func)();
sAct(){Func = NULL;}
}sAct;
protected:
sAct *m_Act;
public:
Man(){m_Act = NULL;}
void Move(){;}
void ActionSet(void (Man::*Func)()){;}
};
int main()
{
Man man;
man.ActionSet(&Man::Move);
return 0;
}
ps:請使用最新的編譯器,以方便查錯:
我的vs2008是這麼寫的,明顯多了:
error C2664: “Man::ActionSet”: 不能將參數 1 從“void (__thiscall Man::* )(void)”轉換爲“void (__cdecl *)(void)”