Some help please. I have confused myself with pointers, structures and vectors again, and all references are only aggravating that confusion at this stage. I need a clear, specific answer, please...
I'll start here, in case I am totally wrong right from the very beginning. I make a structure globally, like this;
Then in main I declare it as a vector;Code:// Globals struct a_struct { double x_position; double y_position; };
Here I have had no trouble, as I can pass it to a function to "push_back" the vector with success, like this;Code:int main() { vector<a_struct> position; //...... }
So, that works. But now I want to access the conents of my vector in another function. I run into trouble...Code:// The Function void increase_vec_size(vector<a_struct> *posit) { a_struct temp_struct = {0}; temp_struct.x_position = 10.5; // for example.. temp_struct.y_position = 15.2; posit->push_back(temp_struct); } // And it was called from within main() like this... int main() { //....... increase_vec_size(&position); //...... }
So, how do I access the elements of a vector structure in a function?Code:// The Function void access_vec_elements(vector<a_struct> *posit, size_t vec_sz) { double x_pos; double y_pos; // This does not work... x_pos = posit.at(vec_sz).x_position; y_pos = posit.at(vec_sz).y_position; // Nor does this... x_pos = posit[vec_sz].x_position; y_pos = posit[vec_sz].y_position; // And I certainly am not able to do this... x_pos = posit->at(vec_sz)->x_position; y_pos = posit->at(vec_sz)->y_position; } // And it is called from main() this way... int main() { //....... access_vec_elements(&position, position.size()); //....... }
I can't stand stabbing around randomly trying to figure it out. I honestly can't get it, so I ask for some help. Sorry if it looks really basic.
My sincere thanks in advance for any assistance.



LinkBack URL
About LinkBacks



