Pergunta

Trying to update to a pointer from a function return. Just for background this is a template that acts like the stl vector. 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. 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.

Foi útil?

Solução

I don't think this initializes 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++)

Outras dicas

for(int i,k = 0; i < cnt; i++,k++)

i,k uses comma operator, which evaluate arguments in order, returning result of last one. So this means "declare i, then declare k and set it to 0"

i is not set. So most likely it get some large random value from memory and your loop never run

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top