C++ 使用C+中的静态变量访问数组位置+;
我正在尝试将对象指定给数组位置。位置由包含数组元素数的静态变量(int)给出。tEntities的大小是5,而FfUnsoneSList的大小是4,所以这不是大小问题C++ 使用C+中的静态变量访问数组位置+;,c++,arrays,visual-studio,static,C++,Arrays,Visual Studio,Static,我正在尝试将对象指定给数组位置。位置由包含数组元素数的静态变量(int)给出。tEntities的大小是5,而FfUnsoneSList的大小是4,所以这不是大小问题 if (TEntity::uEntityCount < 5) { iRandFuncList = rand() % (3 + 1); iRandPosX = rand() % (120 + 1); iRandPosY = rand() %
if (TEntity::uEntityCount < 5)
{
iRandFuncList = rand() % (3 + 1);
iRandPosX = rand() % (120 + 1);
iRandPosY = rand() % (30 + 1);
tEntities[TEntity::uEntityCount] = new TEntity((fFuncionesList[iRandFuncList]), iRandPosX, iRandPosY);
}
TEntity(funcEntity *funcs, int x, int y)
{
m_ix = x;
m_iy = y;
m_funcs = funcs;
uEntityCount++;
}
if(TEntity::uEntityCount<5)
{
iRandFuncList=rand()%(3+1);
iRandPosX=rand()%(120+1);
iRandPosY=rand()%(30+1);
tEntities[TEntity::UntityCount]=新TEntity((fFuncionesList[iRandFuncList])、iRandPosX、iRandPosY;
}
tenty(funcEntity*funcs,int x,int y)
{
m_ix=x;
m_iy=y;
m_funcs=funcs;
UntityCount++;
}
我已经尝试过将静态变量的值赋给int变量,它是有效的,我想了解为什么它不能与静态变量一起工作
if (TEntity::uEntityCount < 5)
{
iRandFuncList = rand() % (3 + 1);
iRandPosX = rand() % (120 + 1);
iRandPosY = rand() % (30 + 1);
int pos = TEntity::uEntityCount;
tEntities[pos] = new TEntity((fFuncionesList[iRandFuncList]), iRandPosX, iRandPosY);
}
if(TEntity::uEntityCount<5)
{
iRandFuncList=rand()%(3+1);
iRandPosX=rand()%(120+1);
iRandPosY=rand()%(30+1);
int pos=tenty::outentitycount;
tEntities[pos]=新TEntity((ffunsoneslist[iRandFuncList]),iRandPosX,iRandPosY);
}
先谢谢你 当您调用构造函数时,您的问题就出现了
tEntities[TEntity::uEntityCount] = new TEntity((fFuncionesList[iRandFuncList]),...);
它增加了单位计数
uEntityCount++;
然后将对象指针指定给tEntities[TEntity::uEntityCount]它将被放置在下一个位置,因此如果当前的uEntityCount=4,它将把指针放置在数组外部的uEntityCount=5上
tEntities[TEntity::uEntityCount] = new TEntity((fFuncionesList[iRandFuncList]),...);
它增加了单位计数
uEntityCount++;
然后将对象指针指定给tEntities[TEntity::uEntityCount],它将被放置在下一个位置,因此如果当前的uEntityCount=4,它将把指针放置在数组外部的uEntityCount=5处ffunsonesList的大小是多少?@goodvibration 4它如何失败?你有没有遇到编译错误,一个奇怪输出的运行时错误?
我曾尝试将静态变量的值赋给int变量,它可以工作,我想了解为什么它不能与静态变量一起工作代码>-表示程序的行为未定义,并且在stak上添加另一个变量已经改变了其行为(并且巧合地使其执行了您想要的操作)。如果tenties
的大小为4,则进行计算。看你贴的照片。你在哪一个位置?提示:tEntities[4]
是第五个元素。不管怎样,没有一个简单的答案,这一切都是猜测。祝你好运。ffunsoneslist
的大小是多少?@goodvibration 4它是如何失败的?你有没有遇到编译错误,一个奇怪输出的运行时错误?我曾尝试将静态变量的值赋给int变量,它可以工作,我想了解为什么它不能与静态变量一起工作代码>-表示程序的行为未定义,并且在stak上添加另一个变量已经改变了其行为(并且巧合地使其执行了您想要的操作)。如果tenties
的大小为4,则进行计算。看你贴的照片。你在哪一个位置?提示:tEntities[4]
是第五个元素。不管怎样,没有一个简单的答案,这一切都是猜测。祝你好运。