[英]C++ Pointer returning NULL
Trying to update to a pointer from a function return. 尝试从函数返回值更新为指针。 Just for background this is a template that acts like the stl vector.
仅作为背景,这是一个类似于stl向量的模板。 This is the returning function.
这是返回函数。
////////////////////////////////////////////////////////////////////////////////
//removes an item from the array
const T& remove(int pos)
{
if(pos > cnt)
pos = cnt;
if(pos < 0)
pos = 0;
static T v;
for(int i,k = 0; i < cnt; i++,k++)
{
if(i == pos)
{
v = element[i];
i++;
}
else
element[k] = element[i];
}
cnt--;
return v;
}
/////////////////////////////////////////////////////////////////////////
The pointer variable I am trying to update: 我正在尝试更新的指针变量:
TVector<Member*> members;
Member* backmember;
backmember = members.remove(members.size()-1);
but backmember always returns null. 但是backmember始终返回null。 I am sure I am missing something simple, just not sure what.
我确定我缺少一些简单的东西,只是不确定什么。 Any ideas?
有任何想法吗? Let me know if you have any questions and thanks in advance.
如有任何问题,请提前告诉我。
I don't think this initializes i
: 我不认为这会初始化
i
:
for(int i,k = 0; i < cnt; i++,k++)
Try changing it to this and testing: 尝试将其更改为此并进行测试:
for(int i = 0,k = 0; i < cnt; i++,k++)
for(int i,k = 0; i < cnt; i++,k++)
i,k
uses comma operator, which evaluate arguments in order, returning result of last one. i,k
使用逗号运算符,该运算符按顺序计算参数,返回最后一个的结果。 So this means "declare i, then declare k and set it to 0" 因此,这意味着“声明i,然后声明k并将其设置为0”
i
is not set. i
没有被设定。 So most likely it get some large random value from memory and your loop never run 因此,很可能它会从内存中获取一些较大的随机值,并且循环永远不会运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.