I am very familiar with Java and this is allowed there. However it looks like it’s not with C++. I’m getting an “invalid array assignment” when trying to assign valuesToGrab = updatingValues;.
//these are class attributes
int updatingValues[361] = {0};
int valuesToGrab[361] = {0};
//this is part of a function that is causing an error.
for (unsigned int i=0; i < 10; i++) {
//this fills values with 361 ints, and num_values gets set to 361.
sick_lms.GetSickScan(values,num_values);
//values has 361 ints, but a size of 2882, so I copy all the ints to an array
//of size 361 to "trim" the array.
for(int z = 0; z < num_values; z++){
updatingValues[z] = values[z];
}
//now I want to assign it to valuesToGrab (another program will be
//constantly grabbing this array, and it can't grab it while it's being
//populated above or there will be issues
valuesToGrab = updatingValues; // THROWING ERROR
}
I don’t want to have to iterate through updatingValues and add it to valuesToGrab one by one, but if I have to I will. Is there a way I can assign it in one function with C++?
Thanks,
The standard idiom for copying in C++ is
make sure
updatingValuesis large enough or you will get overruns and bad things will happen.That said in C++ we generally use a std::vector for this sort of task.
I vector does everything an array does (including static initalization in C++11), but has a well define interface. with iterators, size, empty, resize, push_back and more.
http://en.cppreference.com/w/cpp/algorithm/copy
http://en.cppreference.com/w/cpp/container/vector
EDIT
It is also worth noting that you can combine vector and arrays.
and visa-versa