You are using a version without Ads of this website. Please, consider donating:

### Vector Problems. Needing assistance

I am using the following code. to try and build a vector, and then send that vector back to my main.
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````void VectorGrab(vector VF) { vector Blank(3,0.0); vector ResultVector(3,0.0); for(int i=0,j=0;i<100&&j==0;i++){ double x,y,z, MR2, test; x=MaxR*RandNum(); cout<

but i get a no registers fault as it tries to leave the function every time. The error occurs at the last }.
I am really lost. Can anyone explain what I should be looking for or help me find out what I am doing wrong. I can post more of the code on request.
How was VF defined? Also you are trying to access your ResultVector and Blank vectors out of bounds. When you declared these vectors you created three elements, but you are trying to access element 4. Remember in C++ arrays and vectors are zero based so the valid elements are 0 to size -1.

I have changed the code to reflect the correct indecies and it appears to work. Thank you so much for the help.
-J
P.S. VF was a vector<double> passed into the function (I think i did it right) by reference.
 P.S. VF was a vector passed into the function (I think i did it right) by reference.

Actually you passed this vector by value, not by reference. So any changes you make to this vector in the function will be lost when the function returns.
I am running into that problem now. How can i correct that. I don't really understand the difference in syntax for the two, though I think I understand what each is conceptually supposed to do. Thanks for you help.
`void VectorGrab(vector<double> &VF)`
One last question if anyone wants to answer it. I have come up on another problem that relates to my lack of understanding in vectors.
In the following, it runs fine up until I get to dropping the obtained value into FinalList. At this point it has problems. I believe this is because the vector FinalList has no size, so the position 0,0 doesn't technically exist. My question is, as I do not know how long the final size will need to be, and the operator will likely now know either, how can I re size it to expand by 1 row of 3 units each time I drop data into it? I have been reading about resize, but I am still unclear on the syntax to use. Thanks
 ``123456789101112131415161718192021222324`` ``````int main() { srand(time(0)); bool VT=false; vector< vector > FinalList; for(int i=0, j=0; i<100000&&j<10;i++){ vector VTest(3,0.0); cout<

P.S. If my though on the problem is not correct, please educate me. I am really struggling with this.
Last edited on
 I believe this is because the vector FinalList has no size,

That is correct. When dealing with vectors the element must exist before you can assign a value to an element. To do this you usually use the push_back() method or initialize the vector with the size parameter.

 ``1234567891011121314`` ``````std::vector mints; // Create a vector of ints. // Now add some elements with the push_back() method. mints.push_back(3) mints.push_back(2); mints.push_back(100); std::vector> m2dints; // Create an empty vector vector int. // Now add an element to this vector. This vector will now contain one vector, that has 3 elements. m2dints.push_back(mints); // Now add another element to the mints vector. mints.push_back(1000); // Now add another element to the 2d vector. Now there will be two elements in this 2d vector that contain // 3 and 4 elements. m2dints.push_back(mints);``````

Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: