I was asked to use pointers to add a vector that I would like to pass from some existing function to another function. I really got stuck on how to get the information back from this pointer. I tried a number of things that I read here and there, so let me demonstrate what I'm talking about.
main program:
std::vector<float> * dvertex=NULL; track.calculate(irrelevant stuff, dvertex)
secondary program (track, calculation)
track::caclulate(irrelevant stuff, vector<float> * dvertex) { ... vector<float> pos; ... pos filled after some calculations if(! (dvertex==NULL)) { dvertex = &pos1; }
go back to the core, if I didn’t mix something up above, here are some things I tried
one
(*dvertex).at(0) float z = (*dvertex).at(0)
2
(*dvertex)[0]
and a bunch of things that just didn't compile. I'm pretty stuck as I'm not sure how to get specific values from this vector in the main program. I even thought that it could be the if (! (Dvertex == NULL)) bit, so I changed it to if (dvertex == NULL), but still is not happy. Any help would be greatly appreciated.
* Change / Update * Thanks a lot to everyone for their help, but I'm afraid that everything is still wrong.
So, following the recommendations that I just passed the link: I did this:
primary
std::vector<float> dvertex; track.calculate( foo, &dvertex);
secondary remained unchanged (with verification!)
primary
std::cout<<dvertex[0]<<std:endl;
(among other attempts to actually use the data)
Thanks so much for any thoughts on what I'm still doing wrong. Everything compiles, the program simply freezes when it reaches the point at which data from dvertex is used.
Edit: final fix
in the secondary program that I needed
*dvertex = pos1;
instead
dvertex = &pos1;