How would I get my variables out of a vector?
I can’t use the binary insertion operators or the equal operators.
Earlier, I declared a vector<someStruct> *vObj and allocated it, then returned the vObj and called it in this function:
vector<sameStruct> firstFunc(); for (unsigned int x = 0; x < v->size(); x++) { v[x]; }
when I debug it, v[x] now has the full contents of the original vector, as it did before without the subscript/index.
But I don’t think I’ve done anything to progress beyond that.
I just have about 4 variables inside my vector; when I debug it, it has the information that I need, but I can’t get to it.
As it is written v is a pointer to a vector of structs.
When you index directly into v all you are doing is pointer arithmatic. v[x] is the vector of structs at position x (assuming that v is an array if it is just a single object at the end of the pointer then v[x] for x>0 is just garbage). This is because it is applying the [x] not to the vector pointed to by v but to the pointer v itself.
You need to dereference the pointer and then index into the vector using something like:
At this point you have a reference to the object at the xth position of the vector to get at its member functions / variables use:
or
If you do not want to dereference the vector then access the element within it you might try something like:
This way you are accessing a member function of an object in exactly the same manner as when you called:
I hope that this helps.