vendredi 4 janvier 2019

Why is only the last object of my vector getting its parameter filled?

I'm filling a class std::vector with 5 objects of the same class, but only the last object of the vector is getting its parameters right.

At first I was trying to create 10 objects but only the last 2 got filled, with 5 objectos only 1 object is filled.

std::vector<Entity> Monsters;
for(int i = 0; i < 5; i++){
    int health = distr_health(eng);
    int damage = distr_damage(eng);
    std::string name = monsters[distr_name(eng)];
    Monsters.emplace_back(name,health,damage);
    std::cout<< "Vector size: "<<Monsters.size()<<std::endl;
}

for(auto& monster : Monsters ){
    std::cout << monster.getDamage()<<""<<std::endl;
}

The program should be giving me 5 random numbers but it always gives me zeros for the 4 first objects and a right number on the fifth:

0
0
0
0
31




Aucun commentaire:

Enregistrer un commentaire