Vector losing items

I had a piece of code that worked fine with arrays but isn't when I switched over to vectors. Essentially I create a vector in the main method and then pass it to another function to fill it. When the function is finished, though, it is empty. Presumably the objects are falling out of scope, but why didn't this happen with arrays? And how can I keep them in scope?

A rough outline of the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14

main.cpp:

vector<Animation*> animdb;
Fileio::buildAnimDB(animdb);

//animdb comes back with size and capacity of 0

Fileio.cpp:

void Fileio::buildAnimDB(vector<Animation*> animdb){ 
//vector is properly filled and formatted at the end of this function, size and capacity are what is expected.
}
In function buildAnimDB parameter vector<Animation*> animdb is not the same as declared in main vector vector<Animation*> animdb; though their names coinside.

You should declare the parameter of the function as reference to the vector

void Fileio::buildAnimDB(vector<Animation*> &animdb);

In this case any changes with vector declared as the parameter will be reflected in the original vector.
Ah, that fixed it, thanks. I forget that vectors are not inherently references like arrays are.
Topic archived. No new replies allowed.