【发布时间】:2016-05-17 14:01:27
【问题描述】:
我正在开发一款带班级的 rpg 游戏 我用 ActionList* 创建了一个结构调用 Character 来存储实例。 GeneralPlayer 是一个类,仍然有许多其他玩家类继承了它。 这是我的头文件:
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。 -
你的意思是在你调用函数之后,你作为
P传递的变量似乎没有被初始化?请尝试详细说明,也请尝试创建Minimal, Complete, and Verifiable Example 向我们展示。 -
@songyuanyao 对不起,我还是初学者。如果 P[i] 不是 GeneralPlayer*,那么我应该使用什么?
-
如果我上面的评论不是问题(还),如果你有例如构建错误,然后请编辑问题以包含完整且未经编辑的构建日志。如果您还没有这样做,请read about how to ask good questions。
-
你从不需要
static_cast来将指向任何东西的指针转换为void*。这种转换是自动发生的。
标签: c++ class pointers void-pointers