void RandomArray(vector<int>* getalletjes, int grootte)
{
for (int i = 0; i < grootte; i++)
{
*getalletjes[i] = rand() % 101;
}
}
Function call: RandomArray(&getallen, input);
It gives this error:
1 2 3 4 5 6 7 8 9 10 11 12
error C2100: illegal indirection
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
1> d:\program files\microsoft visual studio 9.0\vc\include\vector(562): could be 'std::vector<_Ty> &std::vector<_Ty>::operator =(const std::vector<_Ty> &)'
1> with
1> [
1> _Ty=int
1> ]
1> while trying to match the argument list '(std::vector<_Ty>, int)'
1> with
1> [
1> _Ty=int
1> ]
If I press "continue", the program works fine though. What's wrong with this?
Well, push_back() is used to put something on the end of a vector. It will grow the vector as needed. The vector getallen can be created without a size specifier and grow as needed.
If you don't use push_back(), your function would need to verify that grootte <= getalletjes.size(). Otherwise you risk assigning past the end of the vector.
"Why does it needs (*getalletjes)[i] and so why doesn't my way work?"
It needs (*getalletjes)[i] instead of *getalletjes[i] because the first option: (*getalletjes)[i] finds where getalletjes was pointing to (which alas is a vector), and then chooses the i'th index of this vector. The way you did it: *getalletjes[i] tries to access the i'th element of the pointer (which makes no sense)...and then dereference that. If this were an array, what you did would be fine because an array variable is really just a pointer to the first element of the array...but for a vector, a pointer to a vector object points to the actual vector object itself, so you need to dereference the vector first...I hope that made sense =)
As mentioned above, a way around this confusion is simply to pass the vector by reference, then you can access it like usual.