我正在與類 的RPG遊戲工作我創建了一個結構調用字符與存儲實例的ActionList *。 GeneralPlayer是一個類,其中仍然有許多其他玩家類繼承它。 這是我的頭文件:類的指針void *
class Battle
{
public:
struct Character
{
char type;//monster or player?
bool alive;
void*instance;//pointer to instance
};
Battle(GeneralPlayer*,AbstractMonster*,int,int,int);
Battle(GeneralPlayer*, AbstractMonster*, int, int);
private:
Character *ActionList;
};
我試圖轉換GeneralPlayer *爲void *。然而,似乎代碼不工作,因爲我認爲。 P和M是這些玩家類的指針數組。
Battle::Battle(GeneralPlayer*P, AbstractMonster*M, int a, int b, int c)
{
a = numP;
b = numM;
c = turn_limit;
ActionList = new Character[numP + numM];
P = new GeneralPlayer[numP];
for (int i = 0; i < numP; i++)
{
ActionList[i] = static_cast<void*>(P[i]);
ActionList[i].type = 'p';
}
for (int i = numP; i < numP+numM; i++)
{
ActionList[i] = static_cast<void*>(M[i]);
ActionList[i].type = 'm';
}
}
它一直顯示錯誤C2440。我希望可以解決我的問題,任何人都可以幫助你。
'P [i]'不是'GeneralPlayer *',它是'GeneralPlayer'。 – songyuanyao
你的意思是說,在你調用該函數之後,你以'P'傳遞的變量似乎沒有被初始化?請嘗試詳細說明,也請嘗試創建[最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)向我們展示。 –
@songyuanyao對不起,我還是初學者。如果P [i]不是GeneralPlayer *,那麼我應該使用什麼? –