'this' may not be used in this context

Jun 8, 2012 at 3:09pm
Hello,

I am currently working with my member function definition, which is as following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void IntArray::Insert(int valueToInsert, int indexToInsert = this->GetLength() + 1)
{
    int* changedArray = new int[this->GetLength() + 1];
    for (int i = 0; i < this->GetLength() + 1; ++i)
    {
        if (this->m_arrayPointer[i])
        {
            changedArray[i] = this->m_arrayPointer[i];
        }
    }
    if (indexToInsert != this->GetLength() + 1)
    {
        for (int i = this->GetLength() + 1; i > indexToInsert; --i)
        {
            changedArray[i] = changedArray[i-1];
        }
    }
    changedArray[indexToInsert] = valueToInsert;

    delete[] this->m_arrayPointer;
    m_arrayPointer = changedArray;
    m_arrayLength = this->GetLength();
}


I receive an error when building this code, which is the subject:

'this' may not be used in this context


I think that I would have to check it inside the body of the function by checking if the parameter is null, and then setting valueToInsert to GetLength() + 1 if it is.

Is that the only solution to this?
Jun 8, 2012 at 3:56pm
Which is the line number for the error?
Line 1 looks awkward, as I think the default values need to be compile-time deducible? Nope.

Anyway, you don't need to abuse this-> the way you do.
So maybe a simple solution could be deleting them all.
this-> is useful when a local variable has the same name as a member variable of the class.
Last edited on Jun 8, 2012 at 4:00pm
Jun 8, 2012 at 3:58pm
The error is indeed at line 1. However, I do not think deleting the values in the array would be favorable, as I need to copy them into my new array.
Jun 8, 2012 at 4:02pm
I meant deleting the unnecessary this->'s from the code.
If you don't want to, just delete the first one on line 1 and try if that works.
Jun 8, 2012 at 4:02pm
An alternative is to provide an overloaded function. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class IntArray
{
//...
    void Insert(int valueToInsert);
    void Insert(int valueToInsert, int indexToInsert);
};

void IntArray::Insert(int valueToInsert)
{
    Insert(valueToInsert, GetLength() + 1);
}

void IntArray::Insert(int valueToInsert, int indexToInsert)
{
    // as above
}
Jun 8, 2012 at 4:20pm
@Catfish2,

Which one's are unnecessary? All of them serve a purpose in my eyes.

@kbw,

That is a good alternative!
---

Just out of curiosity, would my alternative work well?
Jun 8, 2012 at 4:34pm
Which one's are unnecessary? All of them serve a purpose in my eyes.

But you don't need them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Numbers
{
    int a, b;

    // this-> needed in here
    void set_a(int a)
    {
        this->a = a;
    }

    // this-> not needed in here
    void set_ab(int to_ab)
    {
        set_a(to_ab);
        b = to_ab;
    }
};
Last edited on Jun 8, 2012 at 4:35pm
Jun 8, 2012 at 5:05pm
First of all I think your function is incorrect and has undefined behavior. Let consider it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void IntArray::Insert(int valueToInsert, int indexToInsert = this->GetLength() + 1)
{
    int* changedArray = new int[this->GetLength() + 1];
    for (int i = 0; i < this->GetLength() + 1; ++i)
    {
        if (this->m_arrayPointer[i])
        {
            changedArray[i] = this->m_arrayPointer[i];
        }
    }
    if (indexToInsert != this->GetLength() + 1)
    {
        for (int i = this->GetLength() + 1; i > indexToInsert; --i)
        {
            changedArray[i] = changedArray[i-1];
        }
    }
    changedArray[indexToInsert] = valueToInsert;

    delete[] this->m_arrayPointer;
    m_arrayPointer = changedArray;
    m_arrayLength = this->GetLength();
}  


Here you are trying to copy non-zero elements of the original array, are not you?

1
2
3
4
5
6
7
    for (int i = 0; i < this->GetLength() + 1; ++i)
    {
        if (this->m_arrayPointer[i])
        {
            changedArray[i] = this->m_arrayPointer[i];
        }
    }


However the original array has no an element with index this->GetLength(). So you have already gotten undefined behavior. Further you are coping non-zero elements but what about other elements of the new array? What values will it have?!

The next loop contains the same bug.

1
2
3
4
        for (int i = this->GetLength() + 1; i > indexToInsert; --i)
        {
            changedArray[i] = changedArray[i-1];
        }


You are trying access memory outside your array because the new array has no an element with index this->GetLength() + 1

And here you did not change the length of the array

m_arrayLength = this->GetLength();

The new one shall be equal to

m_arrayLength = this->GetLength() + 1;

As for the default argument you can use for example std::string::npos
Jun 8, 2012 at 5:51pm
@Catfish2,

Yeah, I guess it's not necessary, but I just like having them there for some reason.

@vlad from moscow,

Good catch that I did not see there.. the length of the array does not refer to the last element's index. As for the last "flaw", I think I am fine because I updated m_arrayPointer to equal the new array, so getting it's length should be fine.
-----

In general, am I not allowed to use the this pointer in my custom parameters?
Last edited on Jun 8, 2012 at 5:51pm
Jun 8, 2012 at 6:16pm
And how do you calculate the length of the array?
Jun 8, 2012 at 6:46pm
After I set it equal to the new array, I calculate the new length with the GetLength() method.
Jun 8, 2012 at 7:03pm
I understand that you use GetLength() method but how does this method calculate new length of the array?!
Jun 8, 2012 at 8:27pm
I set the m_arrayPointer to the new array, the one with the inserted element, and then set m_arrayLength to the length of the new result of this->GetResult(), which gets the length of the new assignment to m_arrayPointer.

And is this this pointer allowed to be used in parameters?

Edit: Oops, I just forgot that calling GetLength() actually returned m_arrayLength.
Last edited on Jun 9, 2012 at 2:08pm
Topic archived. No new replies allowed.