vector size changes in and out of function

I created a vector "entities" inside the object "_main" to hold members of a struct "Entity" that were created by other classes. When I use _main.entities.emplace_back(object);
(object being the new member of the Entity struct), then immediately cout<<_main.entities.size()<<endl; it returns 1, however if I cout the same thing directly after the function in int main(){ it returns 0. I'm so confused! please help :S

I'm sure my answer can be found by googling but I'm still pretty new to c++ and don't really know what key words to use. To be honest this is my first time using vectors or structs.
Here's my code stripped down (I took out all the irrelevant stuff so if you see uninitialized variables it's probably just been initialized in the full code)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
int window_width = 560;
int window_height = 400;
struct Entity{
sf::Sprite sprite;
int x;
int y;
};

class cla_main{
public:
std::vector<Entity> entities;
cla_main(){
}
void draw_sprites(){
std::sort(entities.begin(), entities.end(), [](Entity& a, Entity& b) {
return((a.y*35)+a.x < (b.y*35)+b.x);
});
for(i=0;i<entities.size();i++){//I realize that an iterator is a better way to go, but one problem at a time
cout<<"test"<<endl;
}
entities.clear();
};

};
class cla_object{
public:
int x;
int y;
cla_object(){
x = window_width/4;
y = window_height/2;
}
void process(sf::Sprite &sprite,sf::RenderWindow &window,cla_player &player,cla_main _main){
Entity object;
object.sprite = sprite;
object.x = (int)round(x/16);
object.y = (int)round(y/16);
_main.entities.emplace_back(object);
cout<<_main.entities.size()<<endl;//this returns 1
}
};
//}
int main(){
//{create objects from classes
cla_object obj_test;
cla_main _main;
//}
cout<<"1-"<<_main.entities.size()<<endl;//these return 0
obj_test.process(spr_impassable,window,obj_player1,_main);
cout<<"2-"<<_main.entities.size()<<endl;
_main.draw_sprites();
cout<<"4-"<<_main.entities.size()<<endl;

return 0;
}
void process(sf::Sprite &sprite,sf::RenderWindow &window,cla_player &player,cla_main _main)

Pass the last parameter by reference (put & in front of it), otherwise it will be passed by value and vanish at the end of the routine.
Oh, duh, lol, thank you you're a life saver. Still getting used to the whole pointers and reference things
Topic archived. No new replies allowed.