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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void VectorGrab(vector<double> VF)
{
    vector<double> Blank(3,0.0);
    vector<double> 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<<x<<endl;
        y=MaxR*RandNum();
        cout<<y<<endl;
        z=MaxR*RandNum();
        cout<<z<<endl;
        MR2=pow(MaxR,2);
        test=pow(x,2)+pow(y,2)+pow(z,2);
        if(test<=MR2){
            j++;
            ResultVector[1]=x;
            cout<<ResultVector[1]<<endl;
            ResultVector[2]=y;
            cout<<ResultVector[2]<<endl;
            ResultVector[3]=z;
            cout<<ResultVector[3]<<endl;
            VF.swap(ResultVector);
 //           int a;
 //           VTest.swap(ResultVector);
 //           return ResultVector;
        }
        cout<<ResultVector[1]<<endl;
        cout<<ResultVector[2]<<endl;
        cout<<ResultVector[3]<<endl;
        cout<<VF[1]<<endl;
        cout<<VF[2]<<endl;
        cout<<VF[3]<<endl;
        cout<<Blank[1]<<endl;
        cout<<Blank[2]<<endl;
        cout<<Blank[3]<<endl;
        system("pause");
    }
}

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<double> 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    srand(time(0));
    bool VT=false;
    vector< vector<double> > FinalList;
        for(int i=0, j=0; i<100000&&j<10;i++){
            vector<double> VTest(3,0.0);
            cout<<VTest[1]<<endl;
            VectorGrab(VTest);
            cout<<VTest[1]<<endl;
            VT=comparator(VTest[0],VTest[1],VTest[2]);
            cout<<VTest[0]<<VTest[1]<<VTest[2]<<endl;
            if(VT==true){
                for(int m=0;m<=2;m++){
                    FinalList[j][m]=VTest[m];
                }
                cout<<FinalList[j][0]<<endl;
                cout<<FinalList[j][1]<<endl;
                cout<<FinalList[j][2]<<endl;
                j++;
            }
        }
    return 0;
}


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::vector<int> 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<std::vector<int>> m2dints; // Create an empty vector vector int.
// Now add an element to this vector. This vector will now contain one vector<int>, 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.